Re: [Tutor] Argparse and how best to use it :p:
On 24/10/2013 14:23, Paradox wrote: > > On 10/24/2013 12:50 PM, Dave Angel wrote: >>> >>> Not sure which tutorial you're using, but the refernce page: >>> http://docs.python.org/3.3/library/argparse.html > Actually I am using 2.7.5, the tutorial found here: > http://docs.python.org/2/howto/argparse.html#id1 > Yeah, and I'd just thanked you for supplying version, then got confused anyway in my reply. Sorry. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] checking list position
On 23/10/2013 21:42, Mauricio Villamil wrote: > Hello, > > And thank you for any help. > Welcome to the python-tutor mailinglist. Please use text emalis, not html. Your email program has lost all the indentation below, making your program really hard to follow. > I am writing a little prog. On this part I need to recursively (preferably) > write to a list a value only if it's less than 49, else do an operation to > make it less than 49, then check which is the next available spot on the > list and write there ... so pseudo code would be like: > > def getGM(seed): ## function > > Gnumbers = [5] ## list to write to > for i in Gnumbers: iterate from begining of list to write to it > if (result <= 49): > > result = math.floor(seed * 1.61803398875) ## find value lesser than 49 > place = int(len(Gnumbers)) ## fins how long is > the list so far > Gnumbers[i] = result ## place result > at spot in list > print ("this is place :", place)## see length of > list > else: ## if number bigger than 49 then divide the number to make it > smaller and wrote > result = math.floor(seed / 1.61803398875) ## make number smaller > if (result <= 49):## if right size write to array > Gnumbers[i] = result > else: > getGM(result)## if not divide again ( this could be a dedicated function ? > > > So ..the biggie is knowing all any time which is the next available place > in the list to write to (we don't want to overwirte previous values > To write to the next place in a list, simply use append. Gnumbers.append(result) No clue what the code you posted is supposed to do. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Argparse and how best to use it
On 24/10/2013 23:45, Alex Kleider wrote: On 2013-10-24 08:38, Paradox wrote: I am trying to learn about argparse and how best to incorporate it into my scripts. I am using Python 2.7.5 on Ubuntu 13.10. It seems from the argparse tutorial that all the arguments are processed as global variables. Is that the standard way to do it or do you normally (when doing real projects and not writing tutorials) process the arguments in the main loop of the script? Some one on this list recently recommended docopt rather than argparse. I've been playing with it and find it far superior although there are still a few issues that I haven't figured out. Alex, if you've still problems I'd be inclined to ask on the main Python mailing list as you're far more likely to get answers than here. Failing that how about approach the author direct? From the Development section here https://github.com/docopt/docopt it's vladi...@keleshev.com -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Argparse and how best to use it
On 2013-10-24 08:38, Paradox wrote: I am trying to learn about argparse and how best to incorporate it into my scripts. I am using Python 2.7.5 on Ubuntu 13.10. It seems from the argparse tutorial that all the arguments are processed as global variables. Is that the standard way to do it or do you normally (when doing real projects and not writing tutorials) process the arguments in the main loop of the script? Some one on this list recently recommended docopt rather than argparse. I've been playing with it and find it far superior although there are still a few issues that I haven't figured out. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 3.3 on OpenSUSE 12.3
On Thu, 2013-10-24 at 21:57 +0200, Rafael Knuth wrote: > Hej, > I can't get Python 3.3 up and running (it's properly installed but I > can't launch it), and I was wondering if anyone using OpenSUSE 12.3 > had similar issues. SUSE community folks weren't really able to help > me, so I was wondering I give it a try here. > Thanks, > Rafael Rafael, I have both Python 2.7.3 and 3.3.0 installed on openSUSE 12.3. To use 3.3 I enter python3.3 at the prompt. If I enter python it defaults to 2.7.3 In the shebang #!/usr/bin/env python3.3 -- Jay Lozier jsloz...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] python, xml, mongodb
hello, me again - the guy with a (mis)fortune of having to deal with a lot of company's in and outgoing xml.I guess they just like xml as a data interchange format, its human readable.i've done my task of exporting the entire postgresql database to some prestructured xml, and i guess i've done the job so well, they want me to develop something like an web service, that will be receiving all kinds of hotel related data, no matter what format(csv, xml, txt, maybe even dBase or whatever)from various clients.my service should be parsing the received data in the prestructed xml format, store everything in one xml file per client, then send to some other service.i really love programming in python and struggling my way through streams of data(i don't like the fact that i'm working in a tourism related company, where clients dictate the terms, but i hope i will change my job some day...) i need some help in the idea of the architecture itself, i'm still a novice in python(started 8 months ago), although i manage to do some nice work, i guess i'm stubborn... so - on the input part ---> i'm receiving a lot of data in various formats, that needs to be validated and parsed in a way i can use it to populate my predefined xml elements.i really dunno what format to expect, no details on that, i just know that 'whatever' i receive will be containing some essential data like hotel id's, occupancies, room details(seaview, room service, prices etc...)is there some way to write a unique parser, that will load a file and look for some pattern of data, then grab it?i will really aprecciate any ideas on that input parsing part. next little problem - what type of database should i use to store the data in.i would prefer something where i can set the default template and then just pass the parsed data to it, so my output xml is already half-way formed(for example, i set a column name like Hotel_name, pass all the hotel names to it, hotel_id - where i just pass all the id's etc) so i can just export it and i have my xml that matches the company's template. i know it's unusual to ask for an idea how to approach a problem - but my project manager and head of the company aren't of much use, they are interested only in clients and financial gain, not really helpful.so they pass me a problem and i have to find the best way to do it.it's my first job, i cannot change any of the terms, i can just go along, or refuse to do it - meaning i'm losing the job.so basically you guys are the best help i can get. so please, give me some ideas, or point me in the right direction.i have like three weeks to complete this. i'll understand if all this is too much to ask, no problem. anyway, thanks :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 1:21 PM, Marc Tompkins wrote: > On Thu, Oct 24, 2013 at 11:25 AM, bob gailer wrote: > >> On 10/24/2013 2:09 PM, Danny Yoo wrote: >> >>> Related: I saw a picture the other day on Google+ of an mailing envelope >>> whose zip code was written in scientific notation. >>> >> That;s odd - since ZIP codes are character, not integer, >> > > You'd like to think that, wouldn't you? I've dealt with a disturbing > number of programs (some of them from big enough names that you'd think > they'd know better) that chose to represent zip codes as integers. You'd > think that the fact that some of them have to be zero-filled would be a big > clue... but no. > Zip+4 is a HUGE hassle for programs that have chosen to do things this way... Zip+4 has always been optional, but the 5010 format for healthcare billing now requires that all zip codes (except patient's actual home addresses) be represented in 9 glorious digits... that's made a bunch of people scramble, let me tell you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 11:25 AM, bob gailer wrote: > On 10/24/2013 2:09 PM, Danny Yoo wrote: > >> Related: I saw a picture the other day on Google+ of an mailing envelope >> whose zip code was written in scientific notation. >> > That;s odd - since ZIP codes are character, not integer, > You'd like to think that, wouldn't you? I've dealt with a disturbing number of programs (some of them from big enough names that you'd think they'd know better) that chose to represent zip codes as integers. You'd think that the fact that some of them have to be zero-filled would be a big clue... but no. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 2:25 PM, bob gailer wrote: > That;s odd - since ZIP codes are character, not integer, It's not that odd. US Zip codes are a sequence of digits. I've worked with plenty of databases where ZIP codes are held in Numeric columns. It's not the ideal format for ZIP codes, because it's perfectly valid for ZIP codes to start with a 0, and Canadian zip codes have letters in them, but I see it all the time. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 3.3 on OpenSUSE 12.3
Hej, I can't get Python 3.3 up and running (it's properly installed but I can't launch it), and I was wondering if anyone using OpenSUSE 12.3 had similar issues. SUSE community folks weren't really able to help me, so I was wondering I give it a try here. Thanks, Rafael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Argparse and how best to use it :p:
On 10/24/2013 12:50 PM, Dave Angel wrote: Not sure which tutorial you're using, but the refernce page: http://docs.python.org/3.3/library/argparse.html Actually I am using 2.7.5, the tutorial found here: http://docs.python.org/2/howto/argparse.html#id1 but the problem is the same. import argparse def parse_my_args(): global args parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() This is exactly what I needed to un-stick my thinking about this, thanks! thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On 10/24/2013 2:09 PM, Danny Yoo wrote: Related: I saw a picture the other day on Google+ of an mailing envelope whose zip code was written in scientific notation. That;s odd - since ZIP codes are character, not integer, -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 9:37 AM, Shelby Martin wrote: > Thank you for your replies. I suspect the solution is a bit more advanced > than where I'm at now, which is chapter 2 of a beginner's book. > Probably because talking about the gory details would completely derail the conversation, as I did. :P My apologies. > Not sure why the author chose to use examples using money calculations > when other calculations that don't need rounding would have sufficed. I > will have to revisit this issue when I'm further along. > I think it's fine to do it when you're learning the language. Just be aware of it, and you should be ok. Related: I saw a picture the other day on Google+ of an mailing envelope whose zip code was written in scientific notation. By computer, of course. Wish I could find it again... Knowing the appropriate representations for data is something you'll pick up as you learn a programming language. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and memory allocation
> And does python being an interpreted language have to do anything with this? I have to interrupt: you mean to say: "And does the C implementation of Python (CPython) have to do anything with this?". There are several implementations of Python out there. Not all of them use an interpreter in the sense that you're thinking of. PyPy and Jython are two concrete examples of Python implementations. As soon as there are at least two _implementations_ of a language which use different strategies for evaluation, the term "interpreted language" loses meaning. It you consider more closely, even C and C++ can be evaluated with an interpreter. And interpreting C is not as silly as it sounds at first. Look at Cling, for example. ( http://root.cern.ch/drupal/content/cling) So try to avoid saying that Python is an interpreted language. "Python has an interpreter" would be a better statement. Try to avoid saying the term "interpreted language" too: it's a conceptual type error. Languages have implementations: they are not themselves implementations. Coming back to your original question: take a brief glance at: http://doc.pypy.org/en/latest/translation.html#escape-analysis-and-stack-allocation That is, there are implementations (at least one in PyPy) that will try to automatically do the stack allocation if it can reason that the values won't escape out of the scope. But allocation is an implementation detail that the Python language does not expose to make life nicer for Python programmers. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and memory allocation
On 24/10/13 17:38, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: on. So, is there a concept of heap and stack in python or is it abstracted? Joel and Dave have given you the answers to this bit And does python being an interpreted language have to do anything with this? Nothing at all. In fact you can get interpreters for C (not sure about C++) and Python has its own compiler. So interpreting or compiling makes no significant difference to how memory allocation works. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and memory allocation
On 24/10/2013 12:38, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > Hello All, > > Before starting to learn python, I first learnt C and C++ for a couple of > years. In C/C++, the choice to assign memory during compile time or during > execution time, i.e. assigning memory from the stack or the heap lay with the > programmer right? But in Python, I have only seen examples of using heap > memory in programs. Like list.append(x) and so on. So, is there a concept of > heap and stack in python or is it abstracted? And does python being an > interpreted language have to do anything with this? > > > > Thanks and sorry for the newbie doubt :) > That's what this group is for, welcome. In C and C++, there are 3 kinds of variable lifetime, rather than 2. If a variable is static, it is created and initialized before your program really starts, and lasts till the program goes away. If a variable is automatic (stack-based), it will be created when the function (or even the scope within a function) begins, and destroyed when the function or scope ends. And this is especially important in C/C++ because when a function is recursive, there may be many named variables on the stack with the same name, and independent values. If a data item is heap-based, it is not really a variable at all, but is a block created by malloc() or equivalent, and *pointed to* by a variable. And it's freed when the corresponding free() or equivalent is called. In Python it's all different. There aren't really any static variables that the user defines, though there are plenty of variables that are defined and initialized long before your code starts running. If you want a surprise, try the following simple program some time. import sys print(sys.modules) when I tried that interactively on 2.7, it printed some 240+ names. As for auto versus heap-based, all values you define are both. What's stored in the 'stack' is references to objects, where the objects are allocated on the heap. And those in turn may have references to other objects, and so on. So if you have a variable x which is a list of size 10, then you have a refernce to a list object, which in turn has references to ten other objects. All these objects are reference counted, and when the count reaches zero, they vanish. And for circular links, there's a garbage collector which operates periodically. These references are referred to as bindings in the python literature. The real point is that when you use a value, you don't have to worry about where it'll be stored, but just on how you'll find it again when you need it. You may have a name bound to it, and also a list item. Even if you reuse the name, the list item will still assure you can find it again. Similarly, if you call sorted() on a list of large strings, you get a new list, but the strings are not duplicated, so it's not nearly the duplication it might look like. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
Thank you for your replies. I suspect the solution is a bit more advanced than where I'm at now, which is chapter 2 of a beginner's book. Not sure why the author chose to use examples using money calculations when other calculations that don't need rounding would have sufficed. I will have to revisit this issue when I'm further along. On Wed, Oct 23, 2013 at 8:44 PM, Danny Yoo wrote: > > > > On Wed, Oct 23, 2013 at 4:13 PM, Alan Gauld wrote: > >> On 23/10/13 22:39, Shelby Martin wrote: >> >>> I've looked online but I'm confused - I need to keep it so that the >>> following program limits the output to two decimal places since it deals >>> in currency. >>> >> >> Its generally a bad idea to use floats when dealing with currency since >> they can introduce small errors which can accumulate over time. It's better >> to either use the Decimal module or to convert money to cents/.pennies and >> then convertback to dollars for display purposes. >> > > > Hi Shelby, > > > If you want to read some gory details, you can find the reasons for why > it's unsafe to represent currency with "floating point" numbers. There's a > classic paper called "What Every Computer Scientist Should Know About > Floating-Point Arithmetic": > > http://www.validlab.com/goldberg/paper.pdf > > and you probably don't want to read the whole thing. :P A rough gist of > the problem: traditional, engineering-focused computer math has a > particular trade-off that most programmers do not realize at first: it > trades accuracy for speed. The native hardware of your computer does > calculations in base-2 rather than base-10 arithmetic. Unfortunately, that > means that fractional quantities take a representational hit: your computer > cannot accurately represent certain decimals in base-2. > > It turns out that this floating point arithmetic limitation is not so bad > for a lot of important applications. But it doesn't bode well at all for > applications that deal with money. There are libraries in Python that > avoid using the built-in floating point hardware, such as the Decimal > library that Alan noted. Computations with it are slower, but that's an > acceptable price for getting the dollars and cents right. If we would do > this sort of thing in the real world, we'd use something like the Decimal > library. You'll see equivalent kinds of libraries in other programming > languages, like the BigDecimal class in Java. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] checking list position
Hello, And thank you for any help. I am writing a little prog. On this part I need to recursively (preferably) write to a list a value only if it's less than 49, else do an operation to make it less than 49, then check which is the next available spot on the list and write there ... so pseudo code would be like: def getGM(seed): ## function Gnumbers = [5] ## list to write to for i in Gnumbers: iterate from begining of list to write to it if (result <= 49): result = math.floor(seed * 1.61803398875) ## find value lesser than 49 place = int(len(Gnumbers)) ## fins how long is the list so far Gnumbers[i] = result ## place result at spot in list print ("this is place :", place)## see length of list else: ## if number bigger than 49 then divide the number to make it smaller and wrote result = math.floor(seed / 1.61803398875) ## make number smaller if (result <= 49):## if right size write to array Gnumbers[i] = result else: getGM(result)## if not divide again ( this could be a dedicated function ? So ..the biggie is knowing all any time which is the next available place in the list to write to (we don't want to overwirte previous values Many thanks for suggestions !! M. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Argparse and how best to use it
On 24/10/2013 16:38, Paradox wrote: I am trying to learn about argparse and how best to incorporate it into my scripts. I am using Python 2.7.5 on Ubuntu 13.10. It seems from the argparse tutorial that all the arguments are processed as global variables. Is that the standard way to do it or do you normally (when doing real projects and not writing tutorials) process the arguments in the main loop of the script? thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor You can save yourself much pain by using the third party docopt, it's absolutely awesome. Download from here https://pypi.python.org/pypi/docopt/ -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and memory allocation
On Thu, Oct 24, 2013 at 12:38 PM, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > Hello All, > > Before starting to learn python, I first learnt C and C++ for a couple > of years. In C/C++, the choice to assign memory during compile time or > during execution time, i.e. assigning memory from the stack or the heap lay > with the programmer right? But in Python, I have only seen examples of using > heap memory in programs. Like list.append(x) and so on. So, is there a > concept of heap and stack in python or is it abstracted? And does python > being an interpreted language have to do anything with this? > Python takes care of memory allocation, so you generally don't need to worry about it. It does its own garbage collection. Also, please use text messages when you post here. HTML style mess up a lot of people experience, so you will get fewer people to help you > > > Thanks and sorry for the newbie doubt :) > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Argparse and how best to use it
On 24/10/2013 11:38, Paradox wrote: > I am trying to learn about argparse and how best to incorporate it into > my scripts. I am using Python 2.7.5 on Ubuntu 13.10. Welcome to python, and to python-tutor mailing list. And thanks for using text mode email, and for supplying both your Python version and OS. > > It seems from the argparse tutorial that all the arguments are processed > as global variables. Is that the standard way to do it or do you > normally (when doing real projects and not writing tutorials) process > the arguments in the main loop of the script? Not sure which tutorial you're using, but the refernce page: http://docs.python.org/3.3/library/argparse.html has some examples that are also at top-level, using globals. I wouldn't recommend that, as I try to minimize the number of non-const globals. The only component that might make sense as a global is the result dictionary, 'args' So the first example would become: import argparse def parse_my_args(): global args parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() Technically, I should just return args, but this is the way I do it for my own code. Although args is technically non-const, by making it a global, I am promising myself that nobody else will modify it once set up. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Translate word to number
On 24/10/2013 06:57, ivantham@raspberrypi wrote: > Hi, I'm new to programming. I'm using python 3 and Debian Linux. My > name is Ivan. I'm bad in for loops, I can't complete the following code: Welcome to Python, and to the tutor newsgroup. This is the right place to ask this sort of question. And thanks for using text email instead of html. There are a whole bunch of methods of the str type. You can see them in the interactive interpreter by typing help(str) > >> input_word = input("Word to translate --> ") First thing is to make sure the word is both alphabetic and lowercase: if input_word.isalpha() and input_word.islower(): else; > > How to change this to for loops? >> a, b, c, d, e, f, g, h = 1, 2, 3, 4, 5, 6, 7, 8 >> >> i, j, k, l, m, n, o, p = 9, 10, 11, 12, 13, 14, 15, 16 >> >> q, r, s, t, u, v, w, x, y, z = 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 Not sure what those equal signs are supposed to represent. But if you want to convert "a" to 1, and "b" to 2, etc., look at the ord() function. print(ord("c")) will print a 99. See if you adjust that to be 3 instead. > > How to change the word to number in for loops? >> for words in input_word: If you loop through a string, you get characters, not words. That's probably what you want, but you'd be better off using a better-named variable. Hint. At any time you're not sure what a function returns, you can test it in the interpreter. So try: >>> for x in "abcd": ... print(type(x)) ... print(x) Once you have a letter, you can use the expression you figured out above to convert it to a number. >> words[0] = number[1] # The loops before in a to z >> word += 1 # Change the word from a to z >> number += 1 # And the number to so that 1 = a, 2 = b >> # Then, add the numbers together None of those make any sense to me. I'd just build a list of numbers, and then use the sum() function on it. Assign the result to the new variable 'number'. > > How to take the output to a .txt file with new line for each word with > numbers in this format? > eg. abc = 6 > eg. good = 41 >> delimeter = " = " # for joining the input word and number >> together >> line = input_word + delimeter + number # save as this format You'll need str(number), so you get the number into printable form. > > Thanks, sorry for my English as English is not the main language that I > speak. > -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python and memory allocation
Hello All, Before starting to learn python, I first learnt C and C++ for a couple of years. In C/C++, the choice to assign memory during compile time or during execution time, i.e. assigning memory from the stack or the heap lay with the programmer right? But in Python, I have only seen examples of using heap memory in programs. Like list.append(x) and so on. So, is there a concept of heap and stack in python or is it abstracted? And does python being an interpreted language have to do anything with this? Thanks and sorry for the newbie doubt :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Count Help
On 23/10/2013 22:12, Jackie Canales wrote: > let say i have a file with random letters of A, AB, C, CD, AC, A, D, CD, DD, > C, B, AB, CD, AB > How do i count the occurrence of each individual item. > > def occurence(name): > infile = open('bloodtype1.txt', 'r') > lst = infile.read() read() doesn't return a list, it returns a string. So the name is poorly chosen. Probably you wanted a list, with each item containing one or two letters. You can use split(",") to break the string into a list, based on commas. And you can use strip() on each item of that list to get rid of whitespace. > infile.close() > > print(lst.count('AB')) # 3 > print(lst.count('CD')) # 2 > print(lst.count('DD'))# 1 > > > i know i can do lst.count("AB") it will give me 3 but if i were to do > lst.count("A") it would give me 6 what I am having trouble with is just > getting the occurrence of the A by itself to give me 1 since there is just > one occurrence of it and would need to do the same for c and d with out it > counting the them in the occurrence of AC and DD or CD. When searching the original string, you have that problem. Making an actual list will fix it. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Argparse and how best to use it
I am trying to learn about argparse and how best to incorporate it into my scripts. I am using Python 2.7.5 on Ubuntu 13.10. It seems from the argparse tutorial that all the arguments are processed as global variables. Is that the standard way to do it or do you normally (when doing real projects and not writing tutorials) process the arguments in the main loop of the script? thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] export postgresql to unified, templated xml
Using PostgreSQL XML functions will be more efficient here, check http://stackoverflow.com/questions/4967117/create-xml-from-postgresql/10399117#10399117for examples. You can try to do this in Python (by creating SQL selects for every table and then parsing each return row into XML), but for it to work correctly you would basically have to recreate those functions, which will take more effort than just learning how to use them. Choose right tool for the job :-) Konrad On 23 October 2013 15:47, Ismar Sehic wrote: > Hello all.i have a quite big task, but i have a feeling it could be easily > solved using python. > The thing is, i need to export the whole company DB(Postgresql) to an XML > template provided by another company. > the template looks like this(just a fragment) : > > ?xml version="1.0" encoding="utf-8"?> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ... > > > > i've never done anything like that.i'm familiar with psycopg2 module, > also, i'm familiar with xml parsing(ElementTree, lxml). > is there some way to use the provided xml as a template, then parse all > the data according to the template, what is the moyt pythonic way to do > this? > > Thx. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Count Help
let say i have a file with random letters of A, AB, C, CD, AC, A, D, CD, DD, C, B, AB, CD, AB How do i count the occurrence of each individual item. def occurence(name): infile = open('bloodtype1.txt', 'r') lst = infile.read() infile.close() print(lst.count('AB')) # 3 print(lst.count('CD')) # 2 print(lst.count('DD'))# 1 i know i can do lst.count("AB") it will give me 3 but if i were to do lst.count("A") it would give me 6 what I am having trouble with is just getting the occurrence of the A by itself to give me 1 since there is just one occurrence of it and would need to do the same for c and d with out it counting the them in the occurrence of AC and DD or CD. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Translate word to number
Hi, I'm new to programming. I'm using python 3 and Debian Linux. My name is Ivan. I'm bad in for loops, I can't complete the following code: > input_word = input("Word to translate --> ") How to change this to for loops? > a, b, c, d, e, f, g, h = 1, 2, 3, 4, 5, 6, 7, 8 > > i, j, k, l, m, n, o, p = 9, 10, 11, 12, 13, 14, 15, 16 > > q, r, s, t, u, v, w, x, y, z = 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 How to change the word to number in for loops? > for words in input_word: > words[0] = number[1] # The loops before in a to z > word += 1 # Change the word from a to z > number += 1 # And the number to so that 1 = a, 2 = b > # Then, add the numbers together How to take the output to a .txt file with new line for each word with numbers in this format? eg. abc = 6 eg. good = 41 > delimeter = " = " # for joining the input word and number > together > line = input_word + delimeter + number # save as this format Thanks, sorry for my English as English is not the main language that I speak. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor