JSON-encoding very long iterators
I would like to add the ability to JSONEncode large iterators. Right now there is no way to do this without modifying the code. The JSONEncoder.default() doc string suggests to do this: For example, to support arbitrary iterators, you could implement default like this:: def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o) but this method requires the whole serialized object to fit in memory and it's a good chance that your iterator is an iterator to save on memory in the first place. By changing the code to accept iterators it is then possible to stream json as I did here: http://stackoverflow.com/a/26094558/289240 This would ideal if it were included in the standard library. Is there any reason why it shouldn't be? -- https://mail.python.org/mailman/listinfo/python-list
Programming help
Dear Sir/Madam, Hello! I'm just new to programming and would like to ask for help.. Can you please give me clue how I should start solving the following problem below? -Build a module that contains three functions that do the following: Compute the average of a list of numbers Finds the statistical median value of a list of numbers Finds the mode of a list of numbers Greatly appreciates it! Al _ __ _Alfred CanoyAgana, GuamPacific time[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
problem solving help
Dear Sir/Madam, Hello! I'm just new to programming and would like to ask for help.. Can you please give me clue how I should start solving the following problem below? Write a program that allows you to do the following five operations: Prompt the user to input a list of numbers (Hint: be sure to have a way for the user to indicate that they are done finished providing the list of numbers) Open a file that contains a list of numbers Compute the average, statistical median, and mode of the list of numbers Store your answers in another file Asks the user whether they want to see the answers and if the answer is yes, opens the file and displays the numbers Greatly appreciates it! Al _ __ _Alfred CanoyAgana, GuamPacific time[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-Help] (fwd)
Hello,
I'm just new to programming and would like to ask for help..
Build a module that contains three functions that do the following:
a.. Compute the average of a list of numbers
b.. Finds the statistical median value of a list of numbers
c.. Finds the mode of a list of numbers
Can you please give me clue how I should start solving the following problem
below? Here's the source code that I did so far:
# compute the average of a list of numbers:
# Keeps asking for numbers until 0 is entered
# Prints the average value
count = 0
sum = 0
number = 1
print 'Enter 0 to exit the loop'
while number != 0:
number = input ('Enter a number: ')
count = count + 1
sum = sum + number
count = count -1
print ' The average is:', sum/count
Greatly appreciates it!
Al
_ _
_ _
Alfred Canoy
Agana, Guam
Pacific time
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python-Help ( Mean,Median & Mode)
Hello,
I revised my source code. It was doing great but I'm having problem listing
all the numbers that I'd input. How can I input all the numbers that I
selected? The source code and the output below:
Source code:
# compute the Mean, Median & Mode of a list of numbers:
sum = 0.0
print 'This program will take several numbers then average them'
count = input(' How many numbers would you like to sum: ')
current_count = 0
freq = {}
freq [current_count] = number
while current_count < count:
current_count = current_count + 1
number = input ('Enter a number: ')
print "Number", current_count,":",number
sum = sum + number
print ' The average is:', sum/count
# list the numbers selected by user then gets the median & mode
listNumbers=[]
for list in number:
listNumbers[list]=listNumbers.get(x,0)+1
print listNumbers
freq = {}
current_count(freq)=number
while number != 0:
number = input ('Enter a number: ')
count = count + 1
sum = sum + number
try:
freq[number] += 1
except KeyError:
freq[number] = 1
max = 0
mode = None
for k, v in freq.iteritems():
if v > max:
max = v
mode = k
print mode
Output:
This program will take several numbers then average them
Number 1 : 6
Number 2 : 9
Number 3 : 8
Number 4 : 4
Number 5 : 2
The average is: 5.8
Traceback (most recent call last):
File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "A:\SLP5.py", line 23, in ?
for x in number:
TypeError: iteration over non-sequence
Thank you much!
Al
_ _
_ _
Alfred Canoy
Agana, Guam
Pacific time
[EMAIL PROTECTED]
- Original Message -
From: "Bob Gailer" <[EMAIL PROTECTED]>
To: "Alfred Canoy" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Sunday, December 05, 2004 7:50 PM
Subject: Re: [Python-Help] (fwd)
At 10:07 PM 12/4/2004, Alfred Canoy wrote:
Hello,
I'm just new to programming and would like to ask for help..
Build a module that contains three functions that do the following:
a.. Compute the average of a list of numbers
b.. Finds the statistical median value of a list of numbers
The middle value in a distribution, above and below which lie an equal
number of values.
c.. Finds the mode of a list of numbers
The value or item occurring most frequently in a series of observations or
statistical data.
Can you please give me clue how I should start solving the following
problem
below? Here's the source code that I did so far:
# compute the average of a list of numbers:
# Keeps asking for numbers until 0 is entered
# Prints the average value
count = 0
sum = 0
number = 1
print 'Enter 0 to exit the loop'
while number != 0:
number = input ('Enter a number: ')
count = count + 1
sum = sum + number
count = count -1
print ' The average is:', sum/count
Great start. In addition append each numbers to a list. Then it is easy to
find the middle of the list for the median. Use a dictionary keyed by the
numbers to count their frequency, then find the entry with the highest
frequency.
See http://www.honors.montana.edu/~jjc/easytut/easytut/ for lists and
dictionaries.
Give it a shot, show us what you come up with, and we'll take the next
step.
Bob Gailer
[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell
--
http://mail.python.org/mailman/listinfo/python-list
Re: [Python-Help] Programming help
Please help me out:(.. I've been trying to figure this out for 2 days now..
I don't know what to use to print all the list of numbers. I hve know idea
how should I do this. I tried a lot of trial & error for the the def, dict,
.list( ). have no luck.
I just need to find this out then I'm good to go for the whole thing. I have
save the other test source code for this problem. I just need to combine it
after I figure this out.
Thank you all!
Source code:
# compute the Mean, Median & Mode of a list of numbers:
sum = 0.0
print 'This program will take several numbers then average them'
count = input(' How many numbers would you like to sum: ')
current_count = 0
freq = {}
freq [current_count] = number
while current_count < count:
current_count = current_count + 1
number = input ('Enter a number: ')
print "Number", current_count,":",number
sum = sum + number
print " [x,...,x] ?"
Al
_ _
_ _
Alfred Canoy
Agana, Guam
Pacific time
[EMAIL PROTECTED]
- Original Message -
From: "Matthew Dixon Cowles" <[EMAIL PROTECTED]>
To: "Alfred Canoy" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, December 06, 2004 11:50 AM
Subject: Re: [Python-Help] Programming help
Dear Alfred,
I'm stuck in the end of my source code. I'm trying to print all the
numbers. How can I print all the list of numbers that I selected?
What have you tried and how did the results differ from what you
expected?
Regards,
Matt
--
http://mail.python.org/mailman/listinfo/python-list
Programming help
Hello,
I'm stuck in the end of my source code. I'm trying
to print all the numbers. How can I print all the list of numbers that I
selected?
Source
code:# compute the Mean, Median & Mode of a list of
numbers:sum = 0.0print 'This program will take several numbers
then average them'count = input(' How many numbers would you like to sum:
')current_count = 0freq = {}freq [current_count] =
numberwhile current_count < count:
current_count = current_count + 1 number = input ('Enter a
number: ') print "Number",
current_count,":",number sum = sum +
numberprint " [x,...,x] ?"
Output:
This program will take several numbers then average them
number 1: 2
number 2: 3
number 3: 4
number 4: 2
How can I print: [2,3,4,2]?
Greatly appreciates!
Al _ __ _Alfred CanoyAgana,
GuamPacific time[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list
python programming help
Hello,
Thanks for the input:-) I made a mean, median &
mode program. I'm trying to figure how can I store this program in another file
and opens the file to display. Any idea what to add in my source code.
Thanks!
# compute the Mean, Median & Mode of a list of
numbers:
sum = 0.0
print 'This program will take several numbers then
average them'count = input(' How many numbers would you like to sum:
')current_count = 0
while current_count <
count: current_count =
current_count + 1num_list = []
while len (num_list) <
count: number = input ('Enter a
number: ')
num_list.append(number) print
"Number", current_count,":",number
sum = sum + number
print num_list
def median(alist):
list_of_numbers = alist[:]
list_of_numbers.sort() listLen =
len(list_of_numbers) middleIndex = (listLen -
1)/2 if listLen % 2 ==
1: # odd number of elements.
return middle element return
list_of_numbers[middleIndex]
else: # even number of element.
return average of middle 2
elements return
(list_of_numbers[middleIndex] + list_of_numbers[middleIndex + 1]) / 2.0if
__name__ == '__main__':
def mode(alist): start = 1 current =
0 new = 0 for i in
alist: if alist.count(i)
>
start:
current =
alist.count(i)
start =
current
new = i if new >
1: return
new else:
return "All members of [%s] are modes." %alistprint 'mean:',
sum/countprint 'median:',median (num_list)print 'mode:',mode
(num_list)
Al _ __ _Alfred CanoyAgana,
GuamPacific time[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list
Re: JSON-encoding very long iterators
On Monday, September 29, 2014 7:10:18 PM UTC-7, Ian wrote: > This would cause things that aren't lists to be encoded as lists. > Sometimes that may be desirable, but in general if e.g. a file object > sneaks its way into your JSON encode call, it is more likely correct > to raise an error than to silently encode the file as if it were a > list of strings. So it should not be the default behavior. That said, > it sounds like it could be made easier to enable streaming from > iterators as an option for those cases where it's desired. I added a stream flag (off by default) and also added file streaming (thanks for the idea). https://github.com/Zectbumo/cpython/compare/2.7 What do you think now? -- https://mail.python.org/mailman/listinfo/python-list
Re: JSON-encoding very long iterators
On Wednesday, October 1, 2014 6:07:23 AM UTC-7, Chris Angelico wrote: > On Wed, Oct 1, 2014 at 8:13 PM, Alfred Morgan wrote: > > What do you think now? > > I think that you're adding features to Python 2.7, which isn't getting > new features. That won't be merged into trunk. Does your patch apply > to 3.x? > > ChrisA Thanks Chris, Yes I made changes to 2.7 because I'm not familiar with Python3 yet. Once I get some feedback I was going to see if someone was interested in porting it. If not I will learn and do it myself. The patch will not work on the 3.x code but the code is similar enough to easily port over. -- https://mail.python.org/mailman/listinfo/python-list
Re: JSON-encoding very long iterators
On Wednesday, October 1, 2014 3:55:19 PM UTC-7, Chris Angelico wrote: > At some point, you'll have to port your patch to the latest codebase Okay, done. https://github.com/Zectbumo/cpython/compare/master Iterators for JSON is now Python 3 ready. -- https://mail.python.org/mailman/listinfo/python-list
Re: JSON-encoding very long iterators
Excellent, thank you. http://bugs.python.org/issue14573 -alfred -- https://mail.python.org/mailman/listinfo/python-list
Reading file bit by bit
Hi all. I'm working on something where I need to read a (binary) file bit by bit and do something depending on whether the bit is 0 or 1. Any help on doing the actual file reading is appreciated. Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list
CSV
Why does this not run? It is right out of the CSV file in the Standard Lib. Python ver 3.4.4, 64 bit. import csv """ READ EXCEL FILE """ filename = 'c:\users\user\my documents\Braxton\Excel\personal\bp.csv' with open (filename, newline = ' ') as bp: dialect = csv.excel reader = csv.reader(bp) for row in reader: print (row) Marc -- https://mail.python.org/mailman/listinfo/python-list
Re: complex numbers
J|rgen Exner wrote: > [EMAIL PROTECTED] wrote: >> #python supports complex numbers. > [...] > > So? > >> # Perl doesn't support complex numbers. But there are packages that >> supports it. > > The Math::Complex module is part of the standard installation > already, no need for any "packages" (whatever that might be). 'package' is a keyword, you should know this :-) Thats an important part of most modules. Actually it seems a lot of people use 'module' and 'package' interchangably, both refering to libraries, if you will, the one can include in a script, or another package. -- http://mail.python.org/mailman/listinfo/python-list
Re: right adjusted strings containing umlauts
On Wednesday, August 28, 2013 12:23:12 PM UTC+2, Dave Angel wrote:
> On 28/8/2013 04:01, Kurt Mueller wrote:
> > Because I cannot switch to Python 3 for now my life is not so easy:-)
> > For some text manipulation tasks I need a template to split lines
> > from stdin into a list of strings the way shlex.split() does it.
> > The encoding of the input can vary.
> > For further processing in Python I need the list of strings to be in
> > unicode.
> According to:
>http://docs.python.org/2/library/shlex.html
> """Prior to Python 2.7.3, this module did not support Unicode
> input"""
> I take that to mean that if you upgrade to Python 2.7.3, 2.7.4, or
> 2.7.5, you'll have Unicode support.
I have Python 2.7.3
> Presumably that would mean you could decode the string before calling
> shlex.split().
Yes, see new template.py:
###
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# split lines from stdin into a list of unicode strings
# decode before shlex
# Muk 2013-08-28
# Python 2.7.3
from __future__ import print_function
import sys
import shlex
import chardet
bool_cmnt = True # shlex: skip comments
bool_posx = True # shlex: posix mode (strings in quotes)
for inpt_line in sys.stdin:
print( 'inpt_line=' + repr( inpt_line ) )
enco_type = chardet.detect( inpt_line )[ 'encoding' ] #
{'encoding': 'EUC-JP', 'confidence': 0.99}
print( 'enco_type=' + repr( enco_type ) )
strg_unic = inpt_line.decode( enco_type ) # decode
the input line into unicode
print( 'strg_unic=' + repr( strg_unic ) ) # unicode
input line
try:
strg_inpt = shlex.split( strg_unic, bool_cmnt, bool_posx, ) # check if
shlex works on unicode
except Exception, errr: # usually
'No closing quotation'
print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ),
file=sys.stderr, )
continue
print( 'strg_inpt=' + repr( strg_inpt ) ) # list of
strings
###
$ python -V
Python 2.7.3
$ echo -e "a b c d e\na Ö u 1 2" | template.py
inpt_line='a b c d e\n'
enco_type='ascii'
strg_unic=u'a b c d e\n'
strg_inpt=['a', 'b', 'c', 'd', 'e']
inpt_line='a \xc3\x96 u 1 2\n'
enco_type='utf-8'
strg_unic=u'a \xd6 u 1 2\n'
error=''ascii' codec can't encode character u'\xd6' in position 2: ordinal not
in range(128)' on inpt_line='a Ö u 1 2'
$ echo -e "a b c d e\na Ö u 1 2" | recode utf8..latin9 |
./split_shlex_unicode.py
inpt_line='a b c d e\n'
enco_type='ascii'
strg_unic=u'a b c d e\n'
strg_inpt=['a', 'b', 'c', 'd', 'e']
inpt_line='a \xd6 u 1 2\n'
enco_type='windows-1252'
strg_unic=u'a \xd6 u 1 2\n'
error=''ascii' codec can't encode character u'\xd6' in position 2: ordinal not
in range(128)' on inpt_line='a � u 1 2'
$
As can be seen, shlex does work only with unicode strings decoded from 'ascii'
strings. (Python 2.7.3)
--
Kurt Müller
--
http://mail.python.org/mailman/listinfo/python-list
Re: split lines from stdin into a list of unicode strings
On Wednesday, August 28, 2013 1:13:36 PM UTC+2, Dave Angel wrote: > On 28/8/2013 04:32, Kurt Mueller wrote: > > For some text manipulation tasks I need a template to split lines > > from stdin into a list of strings the way shlex.split() does it. > > The encoding of the input can vary. > Does that mean it'll vary from one run of the program to the next, or > it'll vary from one line to the next? Your code below assumes the > latter. That can greatly increase the unreliability of the already > dubious chardet algorithm. The encoding only varies from one launch to the other. The reason I process each line is memory usage. Option to have a better reliability of chardet: I could read all of the input, save the input lines for further processing in a list, feed the lines into chardet.universaldetector.UniversalDetector.feed()/close()/result() and then decode and split/shlex the lines in the list. That way the chardet oracle would be more reliable, but roughly twice as much memory will be used. > > import chardet > Is this the one ? > https://pypi.python.org/pypi/chardet Yes. > > $ cat | template.py > Why not have a separate filter that converts from a (guessed) encoding > into utf-8, and have the later stage(s) assume utf-8 ? That way, the > filter could be fed clues by the user, or replaced entirely, without > affecting the main code you're working on. Working on UNIX-like systems (I am happy to work in a MSFZ) the processing pipe would be then: cat | recode2utf8 | splitlines.py memory usage 2 * ( plus chardet memory usage ) > Alternatively, just add a commandline argument with the encoding, and > parse it into enco_type. cat | splitlines.py -e latin9 memory usage 1 * or cat | splitlines.py -e $( codingdetect ) memory usage 1 * So, because memory usage is not primary, I think I will go with the option described above. -- Kurt Müller -- http://mail.python.org/mailman/listinfo/python-list
