|
What is the best way to replace multiple variables in a template file with arguments from the command line? Here's what I'm currently doing: code:
code:
code:
code:
|
# ? Sep 1, 2014 18:35 |
|
|
# ? May 8, 2024 22:47 |
|
You're trying to makecode:
code:
code:
|
# ? Sep 1, 2014 19:46 |
|
What you tried first is going through the whole file and doing one replacement while writing each line, then going through the whole file again doing the next replacement while writing each line. Your second try is doing one replacement, then writing the line, then another replacement, then writing the line again, etc. (At least I think, the fact that you have two different filenames suggests maybe some duplication is intended? It's not completely clear what you're trying to do specifically) What you should be doing (if I understand your intention), as supercrooky suggested, is for each line, first do all the replacements, and only then write each line. There shouldn't be any need to loop over the file twice, or to do two writes on each iteration. As a side note, you don't need the io module, the builtin open function is fine. You should also be using the with statement rather than manually closing: Python code:
|
# ? Sep 1, 2014 20:53 |
|
You should use a template engine like jinja2 because they are designed specifically to solve this type of problem.
|
# ? Sep 1, 2014 21:14 |
|
supercrooky posted:
Thanks! This works, except I had to change var1 and var2 to unicode(var1) and unicode(var2).
|
# ? Sep 1, 2014 21:15 |
|
Looking for help parsing XML files. I have long files - I want to pull some of the information into database entries. Example XML: XML code:
Python code:
Python code:
Dominoes fucked around with this message at 02:00 on Sep 2, 2014 |
# ? Sep 2, 2014 01:30 |
|
BigRedDot posted:Oh, I do too, hence the scare quotes. Let's go another direction and make the ultra-flexible enterprise scorer that can perform arbitrary accumulations and transforms on the input with configurable decoders: Let's take a simple problem and rather than taking five minutes to solve it, let's figure out if it's a special case of some much larger set of problems that we might possibly (but probably won't) want to solve at some point in the future. Then let's develop a spec for an extensible framework and class library that can be called upon to solve any of those larger classes of problems using unnecessarily cryptic chains of method calls to execute the same functionality as we could have done yesterday when we started all this. Sorry, but solving a problem that can be solved in 20 lines of code (unless it's running a space shuttle or pacemaker) shouldn't involve opening powerpoint to make a presentation on your proposed "framework." This is why I hate java.
|
# ? Sep 2, 2014 03:48 |
|
Dominoes posted:Looking for help parsing XML files. I have long files - I want to pull some of the information into database entries. Take a look at the XPath examples in the Python docs, they should be able to get you going.
|
# ? Sep 2, 2014 04:16 |
|
Dominoes posted:Looking for help parsing XML files. I have long files - I want to pull some of the information into database entries. I used https://pypi.python.org/pypi/xmltodict some time ago and it worked well. Not sure about its performance on large files. EDIT: Murodese posted:Really, really bad. I was using xmltodict to read 300kb-1.5mb XML files and changing it to use XPath resulted in a speedup of something like 15,000%. Ouch Space Kablooey fucked around with this message at 15:31 on Sep 2, 2014 |
# ? Sep 2, 2014 04:27 |
|
HardDisk posted:I used https://pypi.python.org/pypi/xmltodict some time ago and it worked well. Not sure about its performance on large files. Really, really bad. I was using xmltodict to read 300kb-1.5mb XML files and changing it to use XPath resulted in a speedup of something like 15,000%.
|
# ? Sep 2, 2014 15:23 |
|
KernelSlanders posted:Let's take a simple problem and rather than taking five minutes to solve it, let's figure out if it's a special case of some much larger set of problems that we might possibly (but probably won't) want to solve at some point in the future. Then let's develop a spec for an extensible framework and class library that can be called upon to solve any of those larger classes of problems using unnecessarily cryptic chains of method calls to execute the same functionality as we could have done yesterday when we started all this. I hope you don't think I was being serious... Edit: Well, the docstrings were serious. I do wish more people wrote good docstrings. Edit2: Although, in truth, it's a balance... I mean, if you've never been in the position of thinking "Goddamn I wish I had generalized this more from the start, what a loving pain in the rear end it's going to be now" then I can only assume you haven't been writing software for very long. BigRedDot fucked around with this message at 16:02 on Sep 2, 2014 |
# ? Sep 2, 2014 15:28 |
|
Does anyone have any tips on PDF rendering in Python? I am playing with Report Lab, but it seems slow at about 500ms per page. I still have to do some optimizing, but If anyone knows any good tips or gotchas, it would help.
|
# ? Sep 2, 2014 15:56 |
|
I was doing my scripts using lxml and in some cases I was able to get away with dot notation, but if an element didn't exist you end up with an error so you have to put handling in for it. I found XML annoying to deal with, but maybe because I was using an older python without all the features the XPath docs have. Anywhere I could get json output instead I always opted for that.
|
# ? Sep 2, 2014 16:06 |
|
KernelSlanders posted:This is why I hate java. Anyone who feels a similar way should read execution in the kingdom of nouns.
|
# ? Sep 2, 2014 17:11 |
|
BigRedDot posted:Edit2: Although, in truth, it's a balance... I mean, if you've never been in the position of thinking "Goddamn I wish I had generalized this more from the start, what a loving pain in the rear end it's going to be now" then I can only assume you haven't been writing software for very long. While I get where you're coming from, YAGNI. I have to tell myself this all the time.
|
# ? Sep 2, 2014 17:31 |
|
Hed posted:Take a look at the XPath examples in the Python docs, they should be able to get you going. Python code:
|
# ? Sep 3, 2014 01:05 |
|
Haystack posted:Ok, so that sort of data mashing is not the sort of thing you want to be doing inside of your template (down that path lies php). Get your data into shape, then pass it into the template. In your case, you're basically mashing together two dicts, so here's an outline of what your code code might look like. Welp I did this correctly and my boss thinks it's too complicated so I got chastised for doing it this way. lol
|
# ? Sep 3, 2014 20:13 |
|
I was hoping to look up definitions of words online, and found out about the nltk module. I've tried to install it from pip on Windows and Linux, with Python 2.7.5 and Python 3.3.4, and it completely screws up when I try to import it. It's just dumb grammar stuff like:code:
|
# ? Sep 4, 2014 06:42 |
|
That specific error is because you have a version of nltk intended for python 2 not 3. From what I can tell you'll need to use the current alpha/beta versions for python 3 support, try running pip with the --pre flag so it'll grab pre-releases.
|
# ? Sep 4, 2014 08:57 |
|
Rocko Bonaparte posted:I was hoping to look up definitions of words online, and found out about the nltk module. ... I must have automatic lookups for crazy Scrabble words, dammit! Based on your goal, using the Requests library to send a web query and parse the response from an online dictionary (or finding an API) would likely give you better results.
|
# ? Sep 4, 2014 13:53 |
|
Heh--I figured I was just bitching on the Internet. I didn't actually expect replies!Alligator posted:That specific error is because you have a version of nltk intended for python 2 not 3. From what I can tell you'll need to use the current alpha/beta versions for python 3 support, try running pip with the --pre flag so it'll grab pre-releases. When I try in Python2 it hates me too: code:
onionradish posted:Based on your goal, using the Requests library to send a web query and parse the response from an online dictionary (or finding an API) would likely give you better results. This is probably what I should be doing. It really hadn't occurred to me to just hit up the REST API of some common dictionary site. That's probably why there isn't much about this online.
|
# ? Sep 4, 2014 16:16 |
|
Rocko Bonaparte posted:
Python code:
|
# ? Sep 4, 2014 16:20 |
|
Got another question for you guys. My program is about 99% complete but Im a little confused on one part. I'm trying to sort some key values from a dictionary and the code I have is just not working. All the program does is read text from a file, strip out any punctuation and then return the count for words that are one to 16 characters in length. Python code:
quote:for key in sorted(_dict): wont work. Im getting the following output for my text file which is the declaration of independence. Nothing is sorted, but the counts are correct: Length Count 1 16 10 56 11 34 12 14 13 9 14 7 15 2 16 0 2 267 3 267 4 169 5 140 6 112 7 98 8 69 9 61 Any idea what I am doing wrong?
|
# ? Sep 5, 2014 00:41 |
|
You're sorting those as strings, not as ints. It's sorting correctly for strings. Change the keys in your dict to integers. There's a bunch of other stuff you're doing weirdly but I'm on my phone.
|
# ? Sep 5, 2014 00:47 |
|
It sorted correctly. Your keys are strings.
|
# ? Sep 5, 2014 00:47 |
|
Dren posted:It sorted correctly. Your keys are strings. wow. my god. I cant believe Im that dumb. Thanks!
|
# ? Sep 5, 2014 00:59 |
|
I know it's your homework, so hopefully these offer you some things to try out instead of giving away answers.
|
# ? Sep 5, 2014 06:46 |
|
salisbury shake posted:Anyone have success with bs4 and multiprocessing? In case anyone cares: this happens because bs4.element.Tag objects cannot be pickled when sent to a Pool's workers because of the overridden __getattr__ method. Wrote an adapter that translates bs4 api calls and attribute lookups into the appropriate XPath strings and lxml method calls, and didn't have to rewrite anything in the parser . Got a 10x speed up in initially wrapping str/bytes content along with a 10x speed up when doing tag queries and attribute lookups. No need to use the multiprocessing module at all.
|
# ? Sep 5, 2014 19:33 |
|
sund posted:I know it's your homework, so hopefully these offer you some things to try out instead of giving away answers. I did consider this but I just wanted to get the basics working until I could handle this case. Ill take a look into defaultdict. quote:[*]Consider if there are any easier ways to determine the length of an sequence without walking through each item and tallying them using a for loop. I know that some of this can be accomplished using "If Not In" etc types of statements. Beyond that Im not sure. quote:[*]Consider your choice of a dict to hold the tallies. Your keys are sequential integers, you look up a string representation of them using a function called "switch", then look up the value in a dict associated with the string. There's a much simpler way to accomplish this. Also don't name your things "_dict" or "switch". Yeah the naming could be a bit better and the switch statement I felt was a bit unnecessary if there was a way to access dict[key] values without having to go that route. I just havent thought about it enough. quote:[*]You never use a Counter or the operator module you import. This needs to be removed. I was going to use that as a tally to count up the collection of the number of characters for each word in the text. quote:[*]Look up the docs for the .read() method on your file object as your code is written expecting different behaviour. Ok Ill check this out. quote:[*]Will you ever see different punctuation than what you have listed? The example given for "it's" will fail. A whitelist of alpha characters to keep would be more robust. The library has something useful for that: https://docs.python.org/3.4/library/string.html#string-constants or isalpha(). I've used isalpha() before and I dont know why I didnt think of this earlier. Thanks. quote:[*]It's interesting you're using a generator statement to conditionally filter out punctuation. If you've got a handle on it, see if employing it elsewhere would simplify what you're doing. Can you give me a hint where it might else be used? Thanks again for all the pointers!
|
# ? Sep 5, 2014 21:12 |
|
spiralbrain posted:I did consider this but I just wanted to get the basics working until I could handle this case. Ill take a look into defaultdict. quote:I know that some of this can be accomplished using "If Not In" etc types of statements. Beyond that Im not sure. quote:Yeah the naming could be a bit better and the switch statement I felt was a bit unnecessary if there was a way to access dict[key] values without having to go that route. I just havent thought about it enough. quote:This needs to be removed. I was going to use that as a tally to count up the collection of the number of characters for each word in the text. quote:Ok Ill check this out.
|
# ? Sep 5, 2014 23:30 |
|
salisbury shake posted:In case anyone cares: this happens because bs4.element.Tag objects cannot be pickled when sent to a Pool's workers because of the overridden __getattr__ method. You could also try using ThreadPool, since I'm guessing you're at least partially I/O bound, not CPU bound. ThreadPool has the same interface as Pool, except that it creates threads instead of processes. It's undocumented, for whatever reason.
|
# ? Sep 6, 2014 00:20 |
|
Am I the only one who stopped using defaultdicts? I think there was like one time a few years ago it was easier for me than just using get and setdefault on a normal dict. Ever since then the collections one has sorta fallen out of favor for me.
|
# ? Sep 6, 2014 01:33 |
|
How would you all rate Code Academy's Python course? I'm about 30% through it and I feel like I'm learning it, but at the same time I want to make sure that I'm doing things correctly. I have little programming experience and I want to learn Python easily. The resources in the OP are alright, but I feel like they are very out of date. What would you guys recommend nowadays for Python tutorials?
|
# ? Sep 6, 2014 21:12 |
|
HonorableTB posted:How would you all rate Code Academy's Python course? I'm about 30% through it and I feel like I'm learning it, but at the same time I want to make sure that I'm doing things correctly. I have little programming experience and I want to learn Python easily. The resources in the OP are alright, but I feel like they are very out of date. What would you guys recommend nowadays for Python tutorials? This reminded me that I started a new OP months ago and then forgot... I'll try to get it up this evening or tomorrow. In the meantime check out Learn Python the Hard Way and Think Python.
|
# ? Sep 6, 2014 22:00 |
|
What matters is if you feel you're learning from it. I've seen a lot of people say they want to learn programming but never get anywhere because they get hung up on deciding what language or tutorial to use. Honestly, from what I've seen of the Code Academy type sites, they've all felt a little too hand-holdy and don't explain what's going on. I tried one of them a while back (I think it was Ruby), and it felt like the tutorial maker was telling me "just type this in, don't worry about what it means or how you'd use it in another situation." Then again, I didn't get too far. I learned Python from Think Python, and I liked it a lot. I went through it after I'd graduated with my CS/math degree, but the author uses it for his intro CS classes.
|
# ? Sep 6, 2014 22:02 |
|
HonorableTB posted:How would you all rate Code Academy's Python course? I'm about 30% through it and I feel like I'm learning it, but at the same time I want to make sure that I'm doing things correctly. I have little programming experience and I want to learn Python easily. The resources in the OP are alright, but I feel like they are very out of date. What would you guys recommend nowadays for Python tutorials?
|
# ? Sep 7, 2014 01:01 |
|
code:
|
# ? Sep 7, 2014 06:21 |
|
Python code:
|
# ? Sep 7, 2014 06:24 |
|
Dammit, I knew that. I wasn't trying to slice, I was trying to subtract from the result, and it caught me off guard.
|
# ? Sep 7, 2014 06:24 |
|
|
# ? May 8, 2024 22:47 |
|
nm
|
# ? Sep 7, 2014 06:25 |