Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
f python?
 

Index | Next | Previous | View Flat


xahlee at gmail

Apr 8, 2012, 4:11 AM


Views: 1338
Permalink
f python?

hi guys,

sorry am feeling a bit prolifit lately.

today's show, is: 〈Fuck Python〉
http://xahlee.org/comp/fuck_python.html

------------------------------------
Fuck Python
By Xah Lee, 2012-04-08

fuck Python.

just fucking spend 2 hours and still going.

here's the short story.

so recently i switched to a Windows version of python. Now, Windows
version takes path using win backslash, instead of cygwin slash. This
fucking broke my find/replace scripts that takes a dir level as input.
Because i was counting slashes.

Ok no problem. My sloppiness. After all, my implementation wasn't
portable. So, let's fix it. After a while, discovered there's the
「os.sep」. Ok, replace 「"/"」 to 「os.sep」, done. Then, bang, all hell
went lose. Because, the backslash is used as escape in string, so any
regex that manipulate path got fucked majorly. So, now you need to
find a quoting mechanism. Then, fuck python doc incomprehensible
scattered comp-sci-r-us BNF shit. Then, fuck python for “os.path” and
“os” modules then string object and string functions inconsistent
ball. And FUCK Guido who wants to fuck change python for his idiotic
OOP concept of “elegance” so that some of these are deprecated.

So after several exploration of “repr()”, “format()”, “‹str›.count()”,
“os.path.normpath()”, “re.split()”, “len(re.search().group())” etc,
after a long time, let's use “re.escape()”. 2 hours has passed. Also,
discovered that “os.path.walk” is now deprecated, and one is supposed
to use the sparkling “os.walk”. In the process of refreshing my
python, the “os.path.walk” semantics is really one fucked up fuck.
Meanwhile, the “os.walk” went into incomprehensible OOP object and
iterators fuck.

now, it's close to 3 hours. This fix is supposed to be done in 10 min.
I'd have done it in elisp in just 10 minutes if not for my
waywardness.

This is Before

def process_file(dummy, current_dir, file_list):
current_dir_level = len(re.split("/", current_dir)) -
len(re.split("/", input_dir))
cur_file_level = current_dir_level+1
if min_level <= cur_file_level <= max_level:
for a_file in file_list:
if re.search(r"\.html$", a_file, re.U) and
os.path.isfile(current_dir + "/" + a_file):
replace_string_in_file(current_dir + "/" + a_file)

This is After

def process_file(dummy, current_dir, file_list):
current_dir = os.path.normpath(current_dir)
cur_dir_level = re.sub( "^" + re.escape(input_dir), "",
current_dir).count( os.sep)
cur_file_level = cur_dir_level + 1
if min_level <= cur_file_level <= max_level:
for a_file in file_list:
if re.search(r"\.html$", a_file, re.U) and
os.path.isfile(current_dir + re.escape(os.sep) + a_file):
replace_string_in_file(current_dir + os.sep + a_file)
# print "%d %s" % (cur_file_level, (current_dir + os.sep +
a_file))

Complete File

# -*- coding: utf-8 -*-
# Python

# find & replace strings in a dir

import os, sys, shutil, re

# if this this is not empty, then only these files will be processed
my_files = []

input_dir = "c:/Users/h3/web/xahlee_org/lojban/hrefgram2/"
input_dir = "/cygdrive/c/Users/h3/web/zz"
input_dir = "c:/Users/h3/web/xahlee_org/"

min_level = 2; # files and dirs inside input_dir are level 1.
max_level = 2; # inclusive

print_no_change = False

find_replace_list = [

(
u"""<iframe style="width:100%;border:none" src="http://xahlee.org/
footer.html"></iframe>""",
u"""<iframe style="width:100%;border:none" src="../footer.html"></
iframe>""",
),

]

def replace_string_in_file(file_path):
"Replaces all findStr by repStr in file file_path"
temp_fname = file_path + "~lc~"
backup_fname = file_path + "~bk~"

# print "reading:", file_path
input_file = open(file_path, "rb")
file_content = unicode(input_file.read(), "utf-8")
input_file.close()

num_replaced = 0
for a_pair in find_replace_list:
num_replaced += file_content.count(a_pair[0])
output_text = file_content.replace(a_pair[0], a_pair[1])
file_content = output_text

if num_replaced > 0:
print "◆ ", num_replaced, " ", file_path.replace("\\", "/")
shutil.copy2(file_path, backup_fname)
output_file = open(file_path, "r+b")
output_file.read() # we do this way instead of “os.rename” to
preserve file creation date
output_file.seek(0)
output_file.write(output_text.encode("utf-8"))
output_file.truncate()
output_file.close()
else:
if print_no_change == True:
print "no change:", file_path

# os.remove(file_path)
# os.rename(temp_fname, file_path)

def process_file(dummy, current_dir, file_list):
current_dir = os.path.normpath(current_dir)
cur_dir_level = re.sub( "^" + re.escape(input_dir), "",
current_dir).count( os.sep)
cur_file_level = cur_dir_level + 1
if min_level <= cur_file_level <= max_level:
for a_file in file_list:
if re.search(r"\.html$", a_file, re.U) and
os.path.isfile(current_dir + re.escape(os.sep) + a_file):
replace_string_in_file(current_dir + os.sep + a_file)
# print "%d %s" % (cur_file_level, (current_dir + os.sep +
a_file))

input_dir = os.path.normpath(input_dir)

if (len(my_files) != 0):
for my_file in my_files:
replace_string_in_file(os.path.normpath(my_file) )
else:
os.path.walk(input_dir, process_file, "dummy")

print "Done."

--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
f python? xahlee at gmail Apr 8, 2012, 4:11 AM
    Re: f python? steve+comp.lang.python at pearwood Apr 8, 2012, 4:34 AM
    Re: f python? rosuav at gmail Apr 8, 2012, 4:37 AM
    Re: f python? xahlee at gmail Apr 8, 2012, 5:04 AM
    Re: f python? martin.hellwig at gmail Apr 8, 2012, 6:10 AM
    Re: f python? someone at someplace Apr 8, 2012, 6:11 AM
        Re: f python? darcy at druid Apr 8, 2012, 7:56 AM
    Re: f python? steve+comp.lang.python at pearwood Apr 8, 2012, 6:29 AM
    Re: f python? dmcanzi at uwaterloo Apr 8, 2012, 10:03 AM
        Re: f python? kaz at kylheku Apr 8, 2012, 10:25 AM
        Re: f python? hjp-usenet2 at hjp Apr 8, 2012, 10:32 AM
            Re: f python? kaz at kylheku Apr 8, 2012, 12:14 PM
                Re: f python? rosuav at gmail Apr 8, 2012, 12:27 PM
                Re: f python? bc at freeuk Apr 8, 2012, 3:46 PM
                Re: f python? spamtrap at library Apr 9, 2012, 5:19 AM
                    Re: f python? roy at panix Apr 9, 2012, 5:45 AM
                        Re: f python? kaz at kylheku Apr 9, 2012, 12:00 PM
                    Re: f python? kaz at kylheku Apr 9, 2012, 11:55 AM
                        Re: f python? spamtrap at library Apr 10, 2012, 3:52 AM
                            Re: f python? jeanpierreda at gmail Apr 10, 2012, 7:32 AM
                            Re: f python? bc at freeuk Apr 10, 2012, 12:55 PM
                            Re: f python? rweikusat at mssgmbh Apr 10, 2012, 1:10 PM
                                Re: f python? tjreedy at udel Apr 10, 2012, 4:09 PM
                                Re: f python? spamtrap at library Apr 10, 2012, 6:09 PM
                                    Re: f python? kaz at kylheku Apr 11, 2012, 7:06 AM
                                    Re: f python? rweikusat at mssgmbh Apr 11, 2012, 7:49 AM
                                    Re: f python? pjb at informatimago Apr 11, 2012, 8:32 AM
                                        Re: f python? spamtrap at library Apr 14, 2012, 7:57 PM
                                            Re: f python? ian.g.kelly at gmail Apr 15, 2012, 9:16 AM
                    Re: f python? rweikusat at mssgmbh Apr 9, 2012, 1:20 PM
                        Re: f python? rweikusat at mssgmbh Apr 9, 2012, 1:44 PM
                        Re: f python? spamtrap at library Apr 10, 2012, 3:58 AM
            Re: f python? ian.g.kelly at gmail Apr 8, 2012, 3:32 PM
        Re: f python? jurgenex at hotmail Apr 8, 2012, 10:49 AM
    Re: f python? tjreedy at udel Apr 8, 2012, 10:59 AM
    Re: f python? nobody at nowhere Apr 8, 2012, 4:43 PM
        Re: f python? alex.mizrahi at gmail Apr 8, 2012, 11:51 PM
    Re: f python? drobinow at gmail Apr 8, 2012, 5:04 PM
    Re: f python? d at davea Apr 8, 2012, 5:25 PM
    Re: f python? xahlee at gmail Apr 8, 2012, 10:45 PM
    Re: f python? rosuav at gmail Apr 8, 2012, 11:52 PM
    Re: f python? bahamutzero8825 at gmail Apr 9, 2012, 12:22 AM
    Re: f python? w_a_x_man at yahoo Apr 11, 2012, 3:55 AM
        Re: f python? timr at probo Apr 11, 2012, 10:16 PM
    Re: f python? tjreedy at udel Apr 15, 2012, 3:49 PM
        Re: f python? ian.g.kelly at gmail Apr 15, 2012, 3:59 PM
    Re: f python? tjreedy at udel Apr 15, 2012, 5:59 PM
    Re: f python? briankp at yahoo Nov 12, 2012, 2:07 PM
        Re: f python? d at davea Nov 12, 2012, 2:37 PM
    Re: f python? steve+comp.lang.python at pearwood Nov 12, 2012, 2:48 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.