performance of script to write very long lines of random chars

2013-04-10 Thread gry
Dear pythonistas, I am writing a tiny utility to produce a file consisting of a specified number of lines of a given length of random ascii characters. I am hoping to find a more time and memory efficient way, that is still fairly simple clear, and _pythonic_. I would like to have something

Re: performance of script to write very long lines of random chars

2013-04-10 Thread gry
On Apr 10, 9:52 pm, Michael Torrie torr...@gmail.com wrote: On 04/10/2013 07:21 PM, gry wrote: from sys import stdout from array import array import random nchars = 3200 rows = 10 avail_chrs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

tiny script has memory leak

2012-05-14 Thread gry
sys.version -- '2.6 (r26:66714, Feb 21 2009, 02:16:04) \n[GCC 4.3.2 [gcc-4_3-branch revision 141291]] I thought this script would be very lean and fast, but with a large value for n (like 15), it uses 26G of virtural memory, and things start to crumble. #!/usr/bin/env python '''write a file

Re: Python: Deleting specific words from a file.

2011-09-12 Thread gry
On Sep 9, 2:04 am, Terry Reedy tjre...@udel.edu wrote: On 9/8/2011 9:09 PM, papu wrote: Hello, I have a data file (un-structed messy file) from which I have to scrub specific list of words (delete words). Here is what I am doing but with no result: infile = messy_data_file.txt

replace random matches of regexp

2011-09-08 Thread gry
[Python 2.7] I have a body of text (~1MB) that I need to modify. I need to look for matches of a regular expression and replace a random selection of those matches with a new string. There may be several matches on any line, and a random selection of them should be replaced. The probability of

Re: replace random matches of regexp

2011-09-08 Thread gry
On Sep 8, 3:51 pm, gry georgeryo...@gmail.com wrote: To elaborate(always give example of desired output...) I would hope to get something like: SELECT public.max(PUBLIC.TT.I) AS SEL_0 FROM (SCHM.T RIGHT OUTER JOIN PUBLIC.TT ON (SCHM.T.I IS NULL)) WHERE (NOT(NOT((power(PUBLIC.TT.F, PUBLIC.TT.F

list comprehension to do os.path.split_all ?

2011-07-28 Thread gry
[python 2.7] I have a (linux) pathname that I'd like to split completely into a list of components, e.g.: '/home/gyoung/hacks/pathhack/foo.py' -- ['home', 'gyoung', 'hacks', 'pathhack', 'foo.py'] os.path.split gives me a tuple of dirname,basename, but there's no os.path.split_all function.

[issue12523] 'str' object has no attribute 'more' [/usr/lib/python3.2/asynchat.py|initiate_send|245]

2011-07-09 Thread Gry
New submission from Gry gryll...@gmail.com: Asynchat push() function has a bug which prevents it from functioning. This code worked fine with Python 2. --- # https://github.com/jstoker/BasicBot import asynchat,asyncore,socket class

Re: CPython on the Web

2011-01-04 Thread gry
On Jan 4, 1:11 am, John Nagle na...@animats.com wrote: On 1/1/2011 11:26 PM, azakai wrote: Hello, I hope this will be interesting to people here: CPython running on the web, http://syntensity.com/static/python.html That isn't a new implementation of Python, but rather CPython 2.7.1,

performance of tight loop

2010-12-13 Thread gry
[python-2.4.3, rh CentOS release 5.5 linux, 24 xeon cpu's, 24GB ram] I have a little data generator that I'd like to go faster... any suggestions? maxint is usually 9223372036854775808(max 64bit int), but could occasionally be 99. width is usually 500 or 1600, rows ~ 5000. from random import

regex help: splitting string gets weird groups

2010-04-08 Thread gry
[ python3.1.1, re.__version__='2.2.1' ] I'm trying to use re to split a string into (any number of) pieces of these kinds: 1) contiguous runs of letters 2) contiguous runs of digits 3) single other characters e.g. 555tHe-rain.in#=1234 should give: [555, 'tHe', '-', 'rain', '.', 'in', '#',

Re: regex help: splitting string gets weird groups

2010-04-08 Thread gry
On Apr 8, 3:40 pm, MRAB pyt...@mrabarnett.plus.com wrote: ... Group 1 and group 4 match '='. Group 1 and group 3 match '1234'. If a group matches then any earlier match of that group is discarded, Wow, that makes this much clearer! I wonder if this behaviour shouldn't be mentioned in some

Re: regex help: splitting string gets weird groups

2010-04-08 Thread gry
    s='555tHe-rain.in#=1234'     import re     r=re.compile(r'([a-zA-Z]+|\d+|.)')     r.findall(s)    ['555', 'tHe', '-', 'rain', '.', 'in', '#', '=', '1234'] This is nice and simple and has the invertible property that Patrick mentioned above. Thanks much! --

generators/iterators: filtered random choice

2006-09-15 Thread gry
I want a function (or callable something) that returns a random word meeting a criterion. I can do it like: def random_richer_word(word): '''find a word having a superset of the letters of word''' if len(set(word) == 26): raise WordTooRichException, word while True: w =

Re: parameter files

2006-09-13 Thread gry
Russ wrote: I have a python module (file) that has a set of parameters associated with it. Let's say the module is called code.py. I would like to keep the parameter assignments in a separate file so that I can save a copy for each run without having to save the entire code.py file. Let's say

Re: Seeking regex optimizer

2006-06-19 Thread gry
Kay Schluehr wrote: I have a list of strings ls = [s_1,s_2,...,s_n] and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in [0,...,n]. There might be relations between those strings: s_k.startswith(s_1) - True

Re: Thinking like CS problem I can't solve

2006-05-23 Thread gry
Alex Pavluck wrote: Hello. On page 124 of Thinking like a Computer Scientist. There is an exercise to take the following code and with the use of TRY: / EXCEPT: handle the error. Can somone help me out? Here is the code: def inputNumber(n): if n == 17: raise 'BadNumberError:

Re: Thinking like CS problem I can't solve

2006-05-23 Thread gry
Alex Pavluck wrote: Hello. On page 124 of Thinking like a Computer Scientist. There is an exercise to take the following code and with the use of TRY: / EXCEPT: handle the error. Can somone help me out? Here is the code: def inputNumber(n): if n == 17: raise 'BadNumberError:

Re: --version?

2006-05-02 Thread gry
I agree. The --version option has become quite a de-facto standard in the linux world. In my sys-admin role, I can blithely run initiate_global_thermonuclear_war --version to find what version we have, even if I don't know what it does... python --version would be a very helpful addition.

Re: best way to determine sequence ordering?

2006-04-28 Thread gry
index is about the best you can do with the structure you're using. If you made the items instances of a class, then you could define a __cmp__ member, which would let you do: a=Item('A') b=Item('B') if ab: something The Item class could use any of various means to maintain order information. If

Re: list.clear() missing?!?

2006-04-13 Thread gry
A perspective that I haven't seen raised here is inheritance. I often say mylist = [] if I'm done with the current contents and just want a fresh list. But the cases where I have really needed list.clear [and laboriously looked for it and ended up with del l[:] were when the object was my

Re: converting lists to strings to lists

2006-04-12 Thread gry
Read about string split and join. E.g.: l = '0.87 0.25 0.79' floatlist = [float(s) for s in l.split()] In the other direction: floatlist = [0.87, 0.25, 0.79004] outstring = ' '.join(floatlist) If you need to control the precision(i.e. suppress the 4), read about the

Re: Sorting a list of objects by multiple attributes

2006-04-10 Thread gry
For multiple keys the form is quite analogous: L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) I.e., just return a tuple with the keys in order from your lambda. Such tuples sort nicely. -- George Young -- http://mail.python.org/mailman/listinfo/python-list

Re: how to pipe to variable of a here document

2006-04-10 Thread gry
http://www.python.org/doc/topics/database/ -- http://mail.python.org/mailman/listinfo/python-list

Re: Characters contain themselves?

2006-04-07 Thread gry
In fact, not just characters, but strings contain themselves: 'abc' in 'abc' True This is a very nice(i.e. clear and concise) shortcut for: 'the rain in spain stays mainly'.find('rain') != -1 True Which I always found contorted and awkward. Could you be a bit more concrete about your

Re: glob and curly brackets

2006-04-07 Thread gry
This would indeed be a nice feature. The glob module is only 75 lines of pure python. Perhaps you would like to enhance it? Take a look. -- http://mail.python.org/mailman/listinfo/python-list

Re: don't understand popen2

2006-03-22 Thread gry
Martin P. Hellwig wrote: Hi all, I was doing some popen2 tests so that I'm more comfortable using it. I wrote a little python script to help me test that (testia.py): - someline = raw_input(something:) if someline == 'test': print(yup) else:

Re: String functions: what's the difference?

2006-03-09 Thread gry
First, don't appologize for asking questions. You read, you thought, and you tested. That's more than many people on this list do. Bravo! One suggestion: when asking questions here it's a good idea to always briefly mention which version of python and what platform (linux, windows, etc) you're

pyparsing: crash on empty element

2006-03-06 Thread gry
[python 2.3.3, pyparsing 1.3] I have: def unpack_sql_array(s): # unpack a postgres array, e.g. {'w1','w2','w3'} into a list(str) import pyparsing as pp withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes) withoutquotes = pp.CharsNotIn(',{}') parser =

style question: how to delete all elements in a list

2006-03-06 Thread gry
Just curious about people's sense of style: To delete all the elements of a list, should one do: lst[:] = [] or del(lst[:]) I seem to see the first form much more often in code, but the second one seems more clearly *deleting* elements, and less dangerously mistaken for the completely

Re: Proper class initialization

2006-03-02 Thread gry
Christoph Zwerschke wrote: Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in the following silly example (representative for more: class A:

Re: critique my code, please

2006-02-06 Thread gry
Just a few suggestions: 1) use consistant formatting, preferably something like: http://www.python.org/peps/pep-0008.html E.g.: yesno = {0:No, 1:Yes, True:Yes, False:No} 2) if (isinstance(self.random_seed,str)): s=s+Random Seed: %s\n % self.random_seed else:

Re: Converting date to milliseconds since 1-1-70

2006-01-24 Thread gry
NateM wrote: How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like Date in java? Thanks, Nate The main module for dates and times is datetime; so

Re: check to see if value can be an integer instead of string

2006-01-17 Thread gry
[EMAIL PROTECTED] wrote: Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted as a string, but i need to know if it is possible to be expressed as an

Re: UNIX timestamp from a datetime class

2005-12-06 Thread gry
John Reese wrote: Hi. import time, calendar, datetime n= 1133893540.874922 datetime.datetime.fromtimestamp(n) datetime.datetime(2005, 12, 6, 10, 25, 40, 874922) lt= _ datetime.datetime.utcfromtimestamp(n) datetime.datetime(2005, 12, 6, 18, 25, 40, 874922) gmt= _ So it's easy to

Re: testing '192.168.1.4' is in '192.168.1.0/24' ?

2005-10-24 Thread gry
There was just recently announced -- iplib-0.9: http://groups.google.com/group/comp.lang.python.announce/browse_frm/thread/e289a42714213fb1/ec53921d1545bf69#ec53921d1545bf69 It appears to be pure python and has facilities for dealing with netmasks. (v4 only). -- George --

Re: dictionnaries and lookup tables

2005-10-11 Thread gry
[EMAIL PROTECTED] wrote: Hello, I am considering using dictionnaries as lookup tables e.g. D={0.5:3.9,1.5:4.2,6.5:3} and I would like to have a dictionnary method returning the key and item of the dictionnary whose key is smaller than the input of the method (or =,,=) but maximal (resp.

Re: Newbie Question

2005-08-19 Thread gry
Yes, eval of data from a file is rather risky. Suppose someone gave you a file containing somewhere in the middle: ... 22,44,66,88,asd,asd,23,43,55 os.system('rm -rf *') 33,47,66,88,bsd,bsd,23,99,88 ... This would delete all the files in your directory! The csv module mentioned above is the

Re: stdin - stdout

2005-08-19 Thread gry
import sys for l in sys.stdin: sys.stdout.write(l) -- George -- http://mail.python.org/mailman/listinfo/python-list

Re: Catching stderr output from graphical apps

2005-08-10 Thread gry
Python 2.3.3, Tkinter.__version__'$Revision: 1.177 $' Hmm, the error window pops up with appropriate title, but contains no text. I stuck an unbuffered write to a log file in ErrorPipe.write and got only one line: Traceback (most recent call last):$ Any idea what's wrong? -- George --

Re: Retaining an object

2005-08-09 Thread gry
sysfault wrote: Hello, I have a function which takes a program name, and I'm using os.popen() to open that program via the syntax: os.popen('pidof var_name', 'r'), but as you know var_name is not expanded within single quotes, I tried using double quotes, and such, but no luck. I was looking

Re: socket programming

2005-07-20 Thread gry
What I have done in similar circumstances is put in a random sleep between connections to fool the server's load manager. Something like: .import time .min_pause,max_pause = (5.0, 10.0) #seconds .while True: . time.sleep(random.uniform(min_pause, max_pause)) . do_connection_and_query_stuff()

Re: suggestions invited

2005-06-23 Thread gry
Aditi wrote: hi all...i m a software engg. student completed my 2nd yr...i have been asked to make a project during these summer vacations...and hereby i would like to invite some ideas bout the design and implementation of an APPLICATION MONITORING SYSTEMi have to start from scrach so

Re: regexp for sequence of quoted strings

2005-05-27 Thread gry
PyParsing rocks! Here's what I ended up with: def unpack_sql_array(s): import pyparsing as pp withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes) withoutquotes = pp.CharsNotIn(',') parser = pp.StringStart() + \ pp.Word('{').suppress() + \

regexp for sequence of quoted strings

2005-05-25 Thread gry
I have a string like: {'the','dog\'s','bite'} or maybe: {'the'} or sometimes: {} [FYI: this is postgresql database array field output format] which I'm trying to parse with the re module. A single quoted string would, I think, be: r\{'([^']|\\')*'\} but how do I represent a *sequence* of

Re: Hacking the scope to pieces

2005-05-24 Thread gry
Hugh Macdonald wrote: We're starting to version a number of our python modules here, and I've written a small function that assists with loading the versioned modules... A module would be called something like: myModule_1_0.py In anything that uses it, though, we want to be able to refer

module exports a property instead of a class -- Evil?

2005-04-29 Thread gry
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the values). I do want the thing to fiercely object to assignments or comparisons with inappropriate values.

Re: module exports a property instead of a class -- Evil?

2005-04-29 Thread gry
Hmm, I had no idea that property was a class. It's listed in the library reference manual under builtin-functions. That will certainly make things neater. Thanks! -- George -- http://mail.python.org/mailman/listinfo/python-list

Re: Inelegant

2005-04-14 Thread gry
I sometimes use the implicit literal string concatenation: def SomeFunction(): if SomeCondition: MyString = 'The quick brown fox ' \ 'jumped over the ' \ 'lazy dog' print MyString SomeFunction() The quick brown fox jumped over the lazy dog It

Re: problem with the logic of read files

2005-04-12 Thread gry
[EMAIL PROTECTED] wrote: I am new to python and I am not in computer science. In fact I am a biologist and I ma trying to learn python. So if someone can help me, I will appreciate it. Thanks #!/cbi/prg/python/current/bin/python # -*- coding: iso-8859-1 -*- import sys import os from

Re: formatting file

2005-04-06 Thread gry
SPJ wrote: I am new to python hence posing this question. I have a file with the following format: test11.1-1 installed test11.1-1 update test22.1-1 installed test22.1-2 update I want the file to be formatted in the following way: test11.1-1 1.1-2 test2

Re: shuffle the lines of a large file

2005-03-07 Thread gry
As far as I can tell, what you ultimately want is to be able to extract a random (representative?) subset of sentences. Given the huge size of data, I would suggest not randomizing the file, but randomizing accesses to the file. E.g. (sorry for off-the-cuff pseudo python): [adjust 8196 == 2**13

Re: converting time tuple to datetime struct

2005-03-03 Thread gry
[your %b is supposed to be the abbreviated month name, not the number. Try %m] In [19]: datetime.datetime(*time.strptime(20-3-2005,%d-%m-%Y)[:6]) Out[19]: datetime.datetime(2005, 3, 20, 0, 0) Cheers, George -- http://mail.python.org/mailman/listinfo/python-list

Re: PyAC 0.1.0

2005-03-01 Thread gry
Premshree Pillai wrote: PyAC 0.1.0 (http://sourceforge.net/projects/pyac/) * ignores non-image files * optional arg is_ppt for ordering presentation images (eg., Powerpoint files exported as images) * misc fixes Package here:

Re: Initializing subclasses of tuple

2005-03-01 Thread gry
To inherit from an immutable class, like string or tuple, you need to use the __new__ member, not __init__. See, e.g.: http://www.python.org/2.2.3/descrintro.html#__new__ -- http://mail.python.org/mailman/listinfo/python-list

Re: Postgres COPY Command with python 2.3 pg

2005-02-17 Thread gry
import pg db = pg.DB('bind9', '192.168.192.2', 5432, None, None, 'named', None) db.query('create temp table fffz(i int,t text)') db.query('copy fffz from stdin') db.putline(3\t'the') db.putline(4\t'rain') db.endcopy() db.query('commit') Note that multiple columns must be separated by tabs ('\t')

Re: change windows system path from cygwin python?

2004-12-16 Thread gry
The _winreg api looks helpful; unfortunately, I'm trying to stick to what can be got from the cygwin install -- no _winreg. Simplicity of installation is quite important. (I'm using cygwin to get the xfree86 X-server, which is the whole point of this exercise) I have found the cygwin

change windows system path from cygwin python?

2004-12-15 Thread gry
[Windows XP Pro, cygwin python 2.4, *nix hacker, windows newbie] I want to write some kind of install script for my python app that will add c:\cygwin\usr\bin to the system path. I don't want to walk around to 50 PC's and twiddle through the GUI to: My Computer -- Control Panel -- System --