Re: Convert a scientific notation to decimal number, and still keeping the data format as float64

2019-10-21 Thread Piet van Oostrum
Piet van Oostrum  writes:

> doganad...@gmail.com writes:
>
>> I dont know much about scala actually. I have just have tried to give
>> 0.0001 and it returned a presentation with an 'e' .whereas python takes
>> 0.0001 and gives 0.0001 . it made me think python is better in that
>> specific subject.
>>
>> However, python though starts to give 'e' number when 5 decimals are
>> given as input. Although there can be systems around which are better in
>> this subject other things I can achieve in python overrides some
>> disadvantages.
>
> Yes, I would say Python is more user-friendly in this particular
> example, although both outputs are correct. If I remember correctly,
> Python had an update in the area several years ago to make the output
> for floating-point numbers more user-friendly, (and at the same time
> maybe even more correct).
>
> But these are just choices of the implementers of the language, not
> characteristics of the language itself.

In Python 0.1 => 1e-05, so it just chooses a different point to
switch from pure decimal to scientific notation.
-- 
Pieter van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert a scientific notation to decimal number, and still keeping the data format as float64

2019-10-21 Thread Piet van Oostrum
doganad...@gmail.com writes:

> I dont know much about scala actually. I have just have tried to give
> 0.0001 and it returned a presentation with an 'e' .whereas python takes
> 0.0001 and gives 0.0001 . it made me think python is better in that
> specific subject.
>
> However, python though starts to give 'e' number when 5 decimals are
> given as input. Although there can be systems around which are better in
> this subject other things I can achieve in python overrides some
> disadvantages.

Yes, I would say Python is more user-friendly in this particular example, 
although both outputs are correct. If I remember correctly, Python had an 
update in the area several years ago to make the output for floating-point 
numbers more user-friendly, (and at the same time maybe even more correct).

But these are just choices of the implementers of the language, not 
characteristics of the language itself.
-- 
Pieter van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert a scientific notation to decimal number, and still keeping the data format as float64

2019-10-20 Thread Piet van Oostrum
doganad...@gmail.com writes:

>
> In the meanwhile I have checked Scala , and it's more limited then Python.
> As an example:
> 0.0001
> 1.0E-4: Double
>
Why do you think this means Scala is more limited than Python?
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Jupyter Notebook -> PDF with A4 pages?

2019-10-16 Thread Piet van Oostrum
Martin Schöön  writes:

> Den 2019-10-15 skrev Piet van Oostrum :
>>
>> What does this report? Compare if there is a difference between home and 
>> work.
>>
>> from jupyter_core.paths import jupyter_path
>> print(jupyter_path('notebook','templates'))
>>
> In both cases I get (with different usernames):
>
> /home/username/.local/share/jupyter/notebook/templates
> /usr/local/share/jupyter/notebook/templates
> /usr/share/jupyter/notebook/templates
>
>> And maybe also
>> print(jupyter_path('nbconvert','templates'))
>
> Same as above but with "nbconvert" substituting "notebook".
>
> Pretty much all jupyter components are of older versions at
> work.

Could that be a version where the configuration is done differently?

> pip is version 8.1.1 which is what Ubuntu 16.04 comes
> with. I have learnt -- the hard way -- that pip should be
> used with the --user option. Does this mean I am stuck with
> pip version 8.1.1? I mean, pip install --user pip seams like
> cheating...

Why should that not work?

> For a moment I thought that maybe pdflatex was missing at work
> but not so.
>
> Disclaimer: I only had a few minutes to spend on this today.
>
> /Martin

Please also try the following command at the shell:
jupyter notebook --generate-config
and see where it generates the config file (this will overwrite the one that 
you made yourself).

Also in the shell where the notebook server is started lokk if the are error 
messages if you generate a PDF from a notebook.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Instantiating sub-class from super

2019-10-16 Thread Piet van Oostrum
DL Neil  writes:

> That said, if a "trans" person has ovaries or testes (for example) then
> a non-traditional sexual identification is irrelevant - for medical
> purposes. Diseases in those areas (and now I'm a long way from a
> research questionnaire and from Python - but this is roughly how it was
> explained to me) still apply, and sadly, may in-fact be considerably
> complicated by any medical processes that may have contributed to a
> transition.

So what if a person has both ovaries and testes?
It is better to keep all options open rather than making hasty subdivisions.
In this context that means attributes (that can be None) rather than subclasses.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Hello, I need help.

2019-10-15 Thread Piet van Oostrum
Damla Pehlivan  writes:

> Dear Python Team,
[...]
>  I am writing this mail quite emotionally. I asked a new "friend" for help,
> but he laughed. He said it was because I am a girl and this is why I could
> not do it. I want to prove to him and the whole world that I can do it. I
> have a lesson next Monday, so could you please help me with my problem?

That is very nasty. Fortunately Python is not gender-sensitive.

What Operating system do you use? I suppose it probably is Windows or Linux.
And did you use the installer from www.python.org?
These have an IDE that is called IDLE. It is simpler than Pycharm, but it can 
do the job. So you can try that. If that also gives an error you could try to 
reinstall Python.

If you are familiar with the command line, then that is also a possibility.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Jupyter Notebook -> PDF with A4 pages?

2019-10-15 Thread Piet van Oostrum
Martin Schöön  writes:

> Den 2019-10-13 skrev Piet van Oostrum :
>> Martin Schöön  writes:
>>
>>> Is there a way to do "Download as PDF" and get A4 pages instead
>>> of Letter? Yes, I know I can do "Download as LaTeX" and edit the
>>
> < snip >
>
>> Make a directory ~/.jupyter/templates and put a file A4article.tplx inside 
>> it:
>>
>> #
>> ((=- Default to the notebook output style -=))
>> ((* if not cell_style is defined *))
>> ((* set cell_style = 'style_jupyter.tplx' *))
>> ((* endif *))
>>
> < snip >
>
>> c.LatexExporter.template_file = 'A4article'
>> c.PDFExporter.latex_count = 3
>> c.PDFExporter.template_file = 'A4article'
>> c.PDFExporter.latex_command = ['pdflatex', '{filename}']
>> #
>> Replace 'pdflatex' with 'xelatex' if you prefer that.
>> You can leave out the c.LatexExporter.template_file line if
>> you don't want the LaTeX exporter to generate A4.
>>
> Thanks a lot.
>
> That worked right away -- at home but not at work. Both are 
> Linux systems but there are differences in both Python and
> LaTeX installations. Shouldn't be too hard to figure out --
> I hope.
>
> /Martin

What does this report? Compare if there is a difference between home and work.

from jupyter_core.paths import jupyter_path
print(jupyter_path('notebook','templates'))

And maybe also
print(jupyter_path('nbconvert','templates'))
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Jupyter Notebook -> PDF with A4 pages?

2019-10-13 Thread Piet van Oostrum
Martin Schöön  writes:

> Is there a way to do "Download as PDF" and get A4 pages instead
> of Letter? Yes, I know I can do "Download as LaTeX" and edit the
> result to get A4 but if there is a setting I have missed I save
> work and time.
>
> Yes, I have looked through the documentation and searched the
> Internet but so far to no avail.
>

Make a directory ~/.jupyter/templates and put a file A4article.tplx inside it:

#
((=- Default to the notebook output style -=))
((* if not cell_style is defined *))
((* set cell_style = 'style_jupyter.tplx' *))
((* endif *))

((=- Inherit from the specified cell style. -=))
((* extends cell_style *))

%===
% Latex Article
%===

((* block docclass *))
\documentclass[a4paper,11pt]{article}
((* endblock docclass *))
#

Make a file ~/.jupyter/jupyter_notebook_config.py

##
import os
custom_path = os.path.expanduser("~/.jupyter/templates")

c = get_config()
c.TemplateExporter.template_path.append(custom_path)
c.LatexExporter.template_file = 'A4article'
c.PDFExporter.latex_count = 3
c.PDFExporter.template_file = 'A4article'
c.PDFExporter.latex_command = ['pdflatex', '{filename}']
#
Replace 'pdflatex' with 'xelatex' if you prefer that.
You can leave out the c.LatexExporter.template_file line if you don't want the 
LaTeX exporter to generate A4.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Strange tab completion oddity with enums?

2019-10-08 Thread Piet van Oostrum
Chris Angelico  writes:

> I'm not sure what's going on here, and it's probably not actually
> enum-specific, but that's where I saw it.
>
> If you create a plain class and have an attribute with an annotation,
> you can see that:
>
>>>> class Foo:
> ... spam: "ham" = 1
> ...
>>>> Foo.__a
> Foo.__abstractmethods__  Foo.__annotations__
>>>> Foo.__annotations__
> {'spam': 'ham'}

Also strange:

It shows Foo.__abstractmethods__ but there is no such attribute.
What's going on?

>>> Foo.__abstractmethods__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: __abstractmethods__

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Print to Paper

2019-10-04 Thread Piet van Oostrum
Daniel  writes:

> How to do a code to print to paper?  please post here a "Hello World"
> code to be printed on paper with an inkjet.
> Thanks
> Daniel

It depends on the Operating System. Python doesn't have a standard, 
system-independent way to print to paper.
On most Unix-like systems (like my MacOS) the following works:

from subprocess import Popen, PIPE
p = Popen('lpr', stdin=PIPE)
p.communicate(b'Hello World')
p.stdin.close()
p.wait()

In a production system error checking should be added. Also if a nice layout is 
desired, you should probably want to generate a PDF file and send that to the 
printer, possibly with the use of LaTeX.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: itertools query

2019-09-27 Thread Piet van Oostrum
Pradeep Patra  writes:

> Hi all,
>
> I have written a small program to generate all the combinations of a and b
> of the array. I want (6,7) tuple also included. Can anybody suggest what
> change I should make to get 6,7 included in my output? Any suggestions
>

Why (6,7)? What about (5,7), (5,8) and (6,8)?
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: CSV reader ignore brackets

2019-09-26 Thread Piet van Oostrum
Skip Montanaro  writes:

> How about just replacing *\(([^)]*)\)* with *"\1"* in a wrapper class's
> line reading method? (I think I have the re syntax approximately right.)
> The csv reader will "just work". Again, nesting parens not allowed.
>
> Skip

here is some working code:

def PReader(csvfile):
import re
for line in csvfile:
line = re.sub(r'\(.*?\)', '"\g<0>"', line)
yield line

import csv
with open('testcsv.csv') as csvfile:
reader = csv.reader(PReader(csvfile), quotechar='"')
    for row in reader:
print(row)


-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most efficient way to replace ", " with "." in a array and/or dataframe

2019-09-23 Thread Piet van Oostrum
Markos  writes:

[...]
>>> Please, any comments or tip?
>> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', 
>> skipinitialspace=True)
>>
> Thank you for the tip.
>
> I didn't realize that I could avoid formatting problems in the dataframe
> or array simply by using the read_csv command with the correct
> parameters (sep and decimal).
>
> I searched for information about the meaning of the letter "b" in the
> parameter decimal=b','  but didn't find.
>
> I found that it also works without the letter b.
>

I added the b because the default in the definition of read_csv is b'.', but 
you are right, it works with just ','.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most efficient way to replace ", " with "." in a array and/or dataframe

2019-09-22 Thread Piet van Oostrum
Markos  writes:

> Hi,
>
> I have a table.csv file with the following structure:
>
> , Polyarene conc ,, mg L-1 ,,,
> Spectrum, Py, Ace, Anth,
> 1, "0,456", "0,120", "0,168"
> 2, "0,456", "0,040", "0,280"
> 3, "0,152", "0,200", "0,280"
>
> I open as dataframe with the command:
>
> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1)
>
[snip]

> Also I'm also wondering if there would be any benefit of making this
> modification in dataframe before extracting the numeric fields to the
> array.
>
> Please, any comments or tip?

data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', 
skipinitialspace=True)

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python is bugging

2019-09-21 Thread Piet van Oostrum
Dave Martin  writes:

> On Saturday, September 21, 2019 at 12:44:27 PM UTC-4, Brian Oney wrote:
>> On Sat, 2019-09-21 at 08:57 -0700, Dave Martin wrote:
>> > On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin
>> > wrote:
>> > > what does expected an indented block
>> > 
>> > *what does an indented block mean?
>> 
>> It means that the line of code belongs to a certain body as defined
>> above its position.  
>> 
>> Please follow the tutorial.
>> 
>> https://docs.python.org/3/tutorial/index.html
>
> df.to_csv(r"faststars.csv", index=None,header=True)
> # starAbsMags=df['radial_velocity']
>
> #GaiaPandasEscapeVelocityCode
>
> import pandas as pd
> import numpy as np
> from astropy.io import fits
> import astropy
> import matplotlib.pyplot as plt
>
>
> #get the combined data and load the fits files
>
> fits_filename="Gaia_DR2/gaiadr2_100pc.fits"
> df=pd.DataFrame()
> with fits.open(fits_filename) as data:
> df=pd.DataFrame(data[1].data)
> df.columns=[c.lower() for c in df.columns]
> print("Columns.")
> print(df.columns.values)
> print("n/n")
> #print out some data meta info to see what we're working with
> print("Number of stars:")
> nstars=len(df)
> print(nstars)
> distances = (df['parallax']/1000)
> starAbsMags =df['phot_g_mean_mag']
> df = df[(df.parallax_over_error > 10 ) ]
> print("Left after filter: " +str(len(df)/float(nstars)*100)+" %")
> df.hist(column='radial_velocity')
> #fastdf=df[(df.radial_velocity > 200) | (df.radial_velocity < -200)]
> fastdf=df[(df.radial_velocity > 550)|(df.radial_velocity<-550)]
> print(len(fastdf))
> #print(fastdf)# starTemps=df['astrometric_weight_al']
> # df.plot.scatter("radial_velocity", "astrometric_weight_al", s=1,
> c="radial_velocity", colormap="plasma")
> # #df=df[(df.radial_velocity>=-550)]
> # #plt.axis([0,400,-800,-550])
> # #plt.axis([0,400,550,800])
> # plt.xlabel('weight(Au)')
> # plt.ylabel('Speed')
> # plt.title('Gaia Speed vs Weight')
>
> this is my code the error is on line 15

1) What is line 15?
2) Always copy/paste the complete error message with your question.
3) Your with body is not indented:

with fits.open(fits_filename) as data:
df=pd.DataFrame(data[1].data)
df.columns=[c.lower() for c in df.columns]
print("Columns.")
print(df.columns.values)

But how should WE know how many lines belong to the body of the with statements?
You should know that and indicate that with the indentation as described in the 
tutorial.

And then there's also a strange line:
c="radial_velocity", colormap="plasma")

Probably meant to be a continuation of the previous, commented line, but as 
written it isn't.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python is bugging

2019-09-21 Thread Piet van Oostrum
Dave Martin  writes:

> On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin wrote:
>> what does expected an indented block
>
> *what does an indented block mean?

From the tutorial, https://docs.python.org/3/tutorial/

3.2. First Steps Towards Programming:

The body of the loop is indented: indentation is Python’s way of grouping 
statements. At the interactive prompt, you have to type a tab or space(s) for 
each indented line. In practice you will prepare more complicated input for 
Python with a text editor; all decent text editors have an auto-indent 
facility. When a compound statement is entered interactively, it must be 
followed by a blank line to indicate completion (since the parser cannot guess 
when you have typed the last line). Note that each line within a basic block 
must be indented by the same amount.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: not working

2019-09-17 Thread Piet van Oostrum
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Supersedes: 
>
> MRAB  writes:
>> >>> import re
>> >>> pattern = r'[0-9]{4,6}'
>> >>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}'
>> >>> re.search(pattern, '1234')
>>
>> >>> re.search(pattern2, '1234')
>>
>>They look the same to me.
>
> |>>> import re
> |>>> pattern  = r'[0-9]{4,6}'
> |>>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}'
> |>>> re.search( pattern,  '1234' ).group( 1 )
> |IndexError: no such group
> |>>> re.search( pattern2, '1234' ).group( 1 )
> |>>>
>
The second pattern has parentheses, hence a group. The first doesn't.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-09-10 Thread Piet van Oostrum
Sayth Renshaw  writes:

>> 
>> That actually creates another error.
>> 
>> A value is trying to be set on a copy of a slice from a DataFrame.
>> Try using .loc[row_indexer,col_indexer] = value instead
>> 
>> See the caveats in the documentation:
>> http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
>> 
>> So tried this
>> df['c'] = df.apply(lambda df1: df1['Current
>> Team'].str.lower().str.strip() == df1['New
>> Team'].str.lower().str.strip(), axis=1)
>> 
>> Based on this SO answer https://stackoverflow.com/a/46570641
>> 
>> Thoughts?
>> 
>> Sayth
>
> This works on an individual row
> df2 = df1.loc[(df1['Current Team'] == df1['New Team']),'D'] = 'Wow'
>
> But how do I apply it to the whole new column and return the new dataset?
>
> Trying to use lambda but it cannot contain assigment
> df2 = df1.apply(lambda df1: [ (df1['Current Team'] == df1['New Team'])  
> ]['D'] = 'succeed')
> df2
>
> Confused
>
> Sayth

df1['Difference'] = df1['Current Team'].str.lower().str.strip() ==
df1['New Team'].str.lower().str.strip()

works on whole columns, not only on an individual row.


xls = pd.ExcelFile("Melbourne.xlsx")
df = xls.parse('Sheet1', skiprows= 4)
df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New 
Team'].str.lower().str.strip()
print(df1.head())

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: phyton

2019-09-10 Thread Piet van Oostrum
tim.g...@quicknet.nl writes:

> Hi everybody,
>
> For school i need to write the right code to get the following outcome.
> Can someone help me with this
> I can't find a solution to link the word high to 1.21.
>
> 11 print(add_vat(101, 'high'))
> 12 print(add_vat(101, 'low'))
>
> Outcome:
>
> 122.21
> 110.09
>
> Thanks!

You could use a dictionary to connect the words to the values.

As this is homework you have to do it yourself. Learn about dictionaries. 
Otherwise just use 'if'.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: numpy array - convert hex to int

2019-09-10 Thread Piet van Oostrum
Sharan Basappa  writes:

> On Sunday, 8 September 2019 11:16:52 UTC-4, Luciano Ramalho  wrote:
>> >>> int('C0FFEE', 16)
>> 12648430
>> 
>> There you go!
>> 
>> On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa  
>> wrote:
>> >
>> > I have a numpy array that has data in the form of hex.
>> > I would like to convert that into decimal/integer.
>> > Need suggestions please.
>> > --
>
> I am sorry. I forgot to mention that I have the data in a numpy array.
> So, when I try to convert to int, I get the following error.
>
> sample code here
> #
> my_data_3 = int(my_data_2)
>
> my_data_4 = my_data_3.astype(np.float)
> #
>
> Error here
> ###
> #np.core.defchararray.replace(my_data_2,",'')
>  27 
> ---> 28 my_data_3 = int(my_data_2)
>  29 
>  30 my_data_4 = my_data_3.astype(np.float)
> TypeError: only length-1 arrays can be converted to Python scalars 
> #

>>> my_data_2 = numpy.array(['0a', '2f', '38', 'ff'])
>>> 
>>> a_toint = np.frompyfunc(lambda x: int(x, 16), 1, 1)
>>> my_data_3 = a_toint(my_data_2)
>>> my_data_3
array([10, 47, 56, 255], dtype=object)

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: issue in handling CSV data

2019-09-10 Thread Piet van Oostrum
Sharan Basappa  writes:

>> 
>> Note that the commas are within the quotes. I'd say Andrea is correct:
>> This is a tab-separated file, not a comma-separated file. But for some
>> reason all fields except the last end with a comma. 
>>

However, genfromtxt is not a full-fledged CSV parser. It does not obey quotes. 
So the commas inside the quotes ARE treated as separators.

> Hi Peter,
>
> I respectfully disagree that it is not a comma separated. Let me explain why.
> If you look the following line in the code, it specifies comma as the 
> delimiter:
>
> 
> my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None)
> 
>
> Now, if you see the print after getting the data, it looks like this:
>
> ## 
> [['"\t"81' '"\t5c'] 
>  ['"\t"04' '"\t11'] 
>  ['"\t"e1' '"\t17'] 
>  ['"\t"6a' '"\t6c'] 
>  ['"\t"53' '"\t69'] 
>  ['"\t"98' '"\t87'] 
>  ['"\t"5c' '"\t4b'] 
> ## 

1) Where did the other fields (address, length) go?
>
> if you observe, the commas have disappeared. That, I think, is because
> it actually treated this as a CSV file.

2) As I said above, if you choose ',' as separator, these will disappear. 
Similarly, if you choose TAB as seperator, the TABs will disappear. As the 
format is a strange mixture of the two, you can use either one. But if it would 
be read with a real CSV-reader, that obeys the quote convention, than using ',' 
as seperator will not work. Only TAB will work.
But in both cases you would have to do some pre- or post-processing to get the 
data as you want them.

> Anyway, I am checking to see if I can discard the tabs and process this.
> I will keep everyone posted.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-09-05 Thread Piet van Oostrum
Piet van Oostrum  writes:

> That would select ROWS 0,1,5,6,7, not columns.
> To select columns 0,1,5,6,7, use two-dimensional indexes
>
> df1 = df.iloc[:, [0,1,5,6,7]]
>
> : selects all rows.

And that also solves your original problem.

This statement:

df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
df1.loc['New Team'].str.lower().str.strip()

should not use .loc, because then you are selecting rows, not columns, but:

df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New 
Team'].str.lower().str.strip()
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-09-05 Thread Piet van Oostrum
Sayth Renshaw  writes:

> On Sunday, 1 September 2019 10:48:54 UTC+10, Sayth Renshaw  wrote:
>> I've created a share doc same structure anon data from my google drive.
>> 
>> https://drive.google.com/file/d/0B28JfFTPNr_lckxQRnFTRF9UTEFYRUVqRWxCNVd1VEZhcVNr/view?usp=sharing
>> 
>> Sayth
>
> I tried creating the df1 dataframe by using iloc instead of loc to avoid any 
> column naming issues.
>
> So i created a list of integers for iloc representing the columns in current 
> example.
>
> df1 = df.iloc[[0,1,5,6,7]] 
>
> However, I ust be misunderstanding the docs
> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc
> Allowed inputs are:
>
> An integer, e.g. 5.
> A list or array of integers, e.g. [4, 3, 0].
>
> Because while it works I appear to grab all columns 13 when I requested 5.
>   UID Name FTE Agent ID Current Leader New Leader Current Team New
> Team Current Site New Site Unnamed: 10 Unnamed: 11 Unnamed: 12
>
> How do I misunderstand iloc?
>
That would select ROWS 0,1,5,6,7, not columns.
To select columns 0,1,5,6,7, use two-dimensional indexes

df1 = df.iloc[:, [0,1,5,6,7]]

: selects all rows.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: IDLE missing !

2019-09-01 Thread Piet van Oostrum
Alex  writes:

> I tried downloading the 3.7.4 version of python and and then tried to
> install tensorflow but found that TF is only available for version
> 3.5.x.
> Thus uninstalled the 3.7.4. version and tried downloading multiple 3.5.x
> version while all of them downloaded but I couldn’d find the IDLE shell
> with them.

Well, on MacOS 10.13.6 (High Sierra) tensorflow 1.14.0 does install on Python 
3.7.4, and it also runs. It does give some warnings about deprecations, however.

There is also a version 2.0RC which installs and runs without warnings.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ``if var'' and ``if var is not None''

2019-09-01 Thread Piet van Oostrum
Hongyi Zhao  writes:

> Hi,
>
> The following two forms are always equivalent:
>
> ``if var'' and ``if var is not None'' 
>
> Regards


In [1]: var = 0

In [2]: if var:
   ...: print('True')
   ...: else:
   ...: print('False')
False

In [3]: if var is not None:
   ...: print('True')
   ...: else:
   ...: print('False')
True

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-08-31 Thread Piet van Oostrum
Sayth Renshaw  writes:

> But on both occasions I receive this error.
>
> # KeyError: 'the label [Current Team] is not in the [index]'
>
> if I test df1 before trying to create the new column it works just fine.
>
What do you mean by testing df1?

And could it be that 'Current Team' is spelled differently in the assignment 
than in the construction of df1? For example a difference in spaces, like a 
triling space or a breaking vs. non-breaking space? Please check that both are 
exactly the same.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: open, close

2019-08-31 Thread Piet van Oostrum
Max Zettlmeißl  writes:

> On Sat, Aug 31, 2019 at 2:22 PM Manfred Lotz  wrote:
>>
>> Could I use the latter as a substitute for the with-construct?
>>
>
> You can't use the second statement as a proper substitute for the first one.
>
> With the context manager, it is ensured that the file is closed. It's
> more or less equal to a "finally" clause which closes the file
> descriptor.
> So as long as the Python runtime environment functions properly, it
> will be closed.
>
> Your second statement on the other hand, is more or less equivalent to:
>
> f = open("foo.txt")
> lines = f.readlines()
>
> Close won't be called.

There is a difference here with the construct that the OP mentioned:
  
  lines = open("foo.txt").readlines()

In that case the file COULD be closed, but there is no guarantee. It depends on 
garbage collection.
In your case the file will not be closed as long as there is still a reference 
to it (as in f). When f disappears and all copies of it as well, the file COULD 
be closed similarly.

On the other hand, the with statement guarantees that the file will be closed, 
so it is the preferred method.

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-08-30 Thread Piet van Oostrum
Sayth Renshaw  writes:

>
> I have tried both
>
> df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')] 
> df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() ==
> df1.loc['New Team'].str.lower().str.strip()
>
> and 
>
> df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
> df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() ==
> df1.loc['New Team'].str.lower().str.strip()
>
> But on both occasions I receive this error.
>
> # KeyError: 'the label [Current Team] is not in the [index]'
>
> if I test df1 before trying to create the new column it works just fine.
>
> Sayth

What does df1.info() produce?
Which versions of numpy, scipy and pandas are you using?

It would be helpful if you could make a copy of the original .xlsx file 
available.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-08-29 Thread Piet van Oostrum
Piet van Oostrum  writes:

> So the correct way to do this is to make df1 a copy rather than a view.
>
> df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]

Or maybe even make an explicit copy:

df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pandas loc on str lower for column comparison

2019-08-29 Thread Piet van Oostrum
Sayth Renshaw  writes:

> Hi
>
> I am importing 4 columns into a dataframe from a spreadsheet.
>
> My goal is to create a 5th column with TRUE or False if column 4 (str) 
> matches column 3.
>
> Trying to leverage this answer https://stackoverflow.com/a/35940955/461887
>
> This is my code 
>
> import pandas as pd
>
> xls = pd.ExcelFile("Melbourne.xlsx")
> df = xls.parse('Sheet1', skiprows= 4)
> df1 = df[['UID','Name','New Leader','Current Team', 'New Team']]
> df1['Difference'] = df1['Current
> Team'].str.lower().str.replace('s/+',"") == df1['New
> Team'].str.lower().str.replace('s/+',"")
>
> Which gives this error
>
> C:\Users\u369811\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:6:
> SettingWithCopyWarning:
> A value is trying to be set on a copy of a slice from a DataFrame.

I think what happens is that df1 is not a copy of the subset of df that you 
want, but it is a VIEW on df instead. This means that df1 shares memory with df 
(for memory savings reasons). But if you would change this view by adding a 
column, where should it be put? In df? If so, where?

So the correct way to do this is to make df1 a copy rather than a view.

df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]

And than it should work.
Except that the str.replace is wrong for what you want.  It just replaces the 
literal string "s/+" with an empty string instead of white space. This was 
wrong in the stackoverflow post.
To replace whitespace it should be str.replace('\\s+',"", regex=True). But 
simpler is to use str.strip():

df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New 
Team'].str.lower().str.strip()
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is augmented assignment of a tuple with iterable unpacking invalid syntax?

2019-05-19 Thread Piet van Oostrum
Eugene Alterman  writes:

> a = 1, 2, 3
>
> b = *a,   # assignment - OK
> b += *a,  # augmented assignment - syntax error
>
> Need to enclose in parenthesis:
>
> b += (*a,)
>
> Why isn't it allowed with an augmented assignment, while it is OK with a
> regular assignment?
>

Syntactically (i.e. according to the grammar):
The right-hand side of an assignment has as one of the alternative a 
starred_expression, which includes starred and unstarred expressions.

The right-hand side of an augmented assignment has an expression_list there. 
(The other option is both cases is a yield_expression.) And if it is a list 
(i.e. there is a comma in it), it is a tuple.

Semantically:
x += y is more or less equivalent to x = x + y, except that x is only evaluated 
once, and y is treated as a unity (think implicit parentheses around it). So 
the y is basically part of an expression. But starred expressions are not 
allowed in expressions, except within explicit parentheses.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Import module from a different subdirectory

2019-05-18 Thread Piet van Oostrum
Rich Shepard  writes:

>
> $ python3
> Python 3.7.3 (default, Mar 26 2019, 06:40:28) [GCC 5.5.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> sys.path
> ['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip',
> '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload',
> '/usr/lib/python3.7/site-packages']
>
> When the gui/ subdirectory is the current working directory and I try to run
> text_act_de.py I get the error that model.py is not found in the classes
> subdirectory. I don't see how my directory structure, file locations, and
> paths differ from your examples.

In a previous message the error was that 'classes' wasn't found, not that 
model.py wasn't found in classes, i.e. it was the same error as Peter got.

So to get this working you must make sure 'classes' is inside a directory that 
is in sys.path, for example by adding:

sys.path.insert(0, '..')
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Testing the data type of a value

2019-05-12 Thread Piet van Oostrum
Luuk  writes:

>
> After thinking about this, (i am prettry new to python), i was doing this:
>
>>>> print(type(5),type(int),type(5)==type(int),type(5)==int)
>   False True
>
> Can someone explain why   type(5)==int   evaluates to   True ?
>
>>> print(int)


The value of int is the class int, which is the class of 5, so type(5) is also 
that same class int.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: WebScrapping

2019-02-19 Thread Piet van Oostrum
Adrian Ordona  writes:

> Hi,
>
> I’m learning how to code and interested in web scrapping to gather data.
> I’m running on Mac OS X 10.9.5 and python 3.7 terminal.
> I’m trying to capture the name of the brand and price but i keep getting an 
> error (see below).
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/anaconda3/lib/python3.7/site-packages/bs4/element.py", line 1884, in 
> __getattr__
> "ResultSet object has no attribute '%s'. You're probably treating a list 
> of items like a single item. Did you call find_all() when you meant to call 
> find()?" % key
> AttributeError: ResultSet object has no attribute 'find'. You're probably 
> treating a list of items like a single item. Did you call find_all() when you 
> meant to call find()?
>
>
> Here’s what i got and thanks for the help
>
> import bs4
> from urllib.request import urlopen as uReq
> from bs4 import BeautifulSoup as soup
>
> my_url = 
> 'https://www.newegg.com/Desktop-Graphics-Cards/SubCategory/ID-48?Tid=7709'
>
> uClient = uReq(my_url)
> page_html = uClient.read()
> uClient.close()
> page_soup = soup(page_html, "html.parser")
> records = []
>
> containers = page_soup.findAll("div",{"class":"item-container"})
>
> for container in containers:
>   brand = container.find('div', attrs={'class':'item-branding'})
>   price = container.find('div', attrs={'class':'item-action'})
>   records.append((brand, price))
>
When I put this in a python file, and run it under python3.7, it works.
It seems you were running this line by line interactively, tight?

Could it be that you accidentally entered
brand = containers.find('div', attrs={'class':'item-branding'})
i.e containers rather than container, because that would generate the error 
that you copied.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert a list with wrong encoding to utf8

2019-02-15 Thread Piet van Oostrum
vergos.niko...@gmail.com writes:

> Τη Πέμπτη, 14 Φεβρουαρίου 2019 - 8:56:31 μ.μ. UTC+2, ο χρήστης MRAB έγραψε:
>
>> It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode 
>> string that was decoded wrongly from the bytes.
>
> Yes it doesnt have the 'b' prefix so that hexadecimal are representation of 
> strings and not representation of bytes.
>
> I just tried:
>
> names = tuple( [s.encode('latin1').decode('utf8') for s in names] )
>
> but i get
> UnicodeEncodeError('latin-1', 'Άκης Τσιάμης', 0, 4, 'ordinal not in 
> range(256)')
>
> 'Άκης Τσιάμης' is a valid name but even so it gives an error.
>
> Is it possible that Python3 a Unicode had the string wrongly decoded from the 
> bytes ?
>
> What can i do to get the names?!

python3

>>> x = '\xce\x86\xce\xba\xce\xb7\xcf\x82 
>>> \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82'
>>> b = bytes(ord(c) for c in x)
>>> b.decode('utf-8')
'Άκης Τσιάμης'
>>> 
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The slash "/" as used in the documentation

2019-02-09 Thread Piet van Oostrum
Christian Gollwitzer  writes:

>>>> __import__( 'sys' ).version
> '3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) \n[GCC
> 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]'
>>>> help( __import__( 'math' ).sin )
>
>
> Help on built-in function sin in module math:
>
> sin(...)
> sin(x)
>
> Return the sine of x (measured in radians).
>
>
> The help is actually not written out to the terminal but, displayed in
> the pager (less), and the first sin(...) is typeset in boldface.
>
>   Christian

Even simpler:

>>> help(len)
help(len)
Help on built-in function len in module builtins:

len(obj, /)
Return the number of items in a container.

both in python 3.6 and 3.7.

This is weird.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The slash "/" as used in the documentation

2019-02-09 Thread Piet van Oostrum
r...@zedat.fu-berlin.de (Stefan Ram) writes:

>   The slash »/« as used in the documentation
>
> f( x, /, y )
>
>   is so ugly, it will disappear. Especially since it consumes
>   a comma as it it was a parameter itself.
>
>   Possible alternatives include: 
>
>   A newline:
>
> f( x,
> y )
>
>   A separate specification of the first non-positional parameter:
>
> f( x, y )
> # y
>
>   (use "(" or ")" when all parameters are non-positional or
>   positional, respectively).
>
>   A semicolon:
>
> f( x; y )
>
>   (the actual call still would use a comma there).
>
What are you talking about? What documentation? It seems to me you are talking 
about a completely different programming language, not python.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python read text file columnwise

2019-01-11 Thread Piet van Oostrum
shibashib...@gmail.com writes:

> Hello
>> 
>> I'm very new in python. I have a file in the format:
>> 
>> 2018-05-31   16:00:0028.90   81.77   4.3
>> 2018-05-31   20:32:0028.17   84.89   4.1
>> 2018-06-20   04:09:0027.36   88.01   4.8
>> 2018-06-20   04:15:0027.31   87.09   4.7
>> 2018-06-28   04.07:0027.87   84.91   5.0
>> 2018-06-29   00.42:0032.20   104.61  4.8
>
> I would like to read this file in python column-wise.  
>
> I tried this way but not working 
>   event_list = open('seismicity_R023E.txt',"r")
> info_event = read(event_list,'%s %s %f %f %f %f\n');

Why would you think that this would work?

See https://docs.python.org/3/library/csv.html

Something like:

#!/usr/bin/env python3

import csv

with open('testcsv.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
for row in reader:
for i in range(2, 5):
row[i] = float(row[i])
print(row)

You could convert the first two columns to datetime format if you wish.
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Facing an Error after migrating from python 3.4.1 to python 3.6.6 ( Failed to import the site module )

2018-12-28 Thread Piet van Oostrum
sandeep.ba...@gmail.com writes:

> ``` 
> Error code: 
> -- 
>
>
> Traceback (most recent call last): 
>   File 
> "C:\Users\sandeep\AppData\Local\Programs\Python\Python36-32\Lib\site.py", 
> line 73, in  import os 
>   File 
> "C:\Users\sandeep\AppData\Local\Programs\Python\Python36-32\Lib\os.py", line 
> 652, in  
> from _collections_abc import MutableMapping 
>   File 
> "C:\Users\sandeep\AppData\Local\Programs\Python\Python36-32\Lib\_collections_abc.py",
>  line 58 
> async def _coro(): pass 
> ^ 
> SyntaxError: invalid syntax 
> Failed to import the site module 
>
>  
> After migrating from python 3.4.1 to python 3.6.6 
> while Executing my project, I'm facing this issue and not able to
> resolve it. Can i get any solution for this issue?

Could it be that your PYTHONPATH environment variable is set to a directory in 
Python 3.4.1?

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with subprocess.Popen and EINTR

2017-10-29 Thread Piet van Oostrum
Cameron Simpson <c...@cskk.id.au> writes:

[...]
>  What if you did this:
>
>  os.signal(SIGINT, SIG_IGN)
>  ... code code code, including the Popen/wait ...
>  old_handler = os.signal(SIGINT, do_nothing_handler)
>  sleep(...)
>  os.signal(SIGINT, old_handler)
>
> SIG_IGN is different from a do-nothing handler; it prevents the process
> seeing the signal at all, and therefore prevents the signal interrupting
> system calls.
>
> So you activate your dummy-quit-sleeping function only around the sleep 
> itself.

Thanks. That is a good idea. I'll try that out.
Or I could first try to switch to Python 3, as Chris suggested, to see if that 
makes the problem disappear.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem with subprocess.Popen and EINTR

2017-10-28 Thread Piet van Oostrum
I am using Python 2.7.14 on MacOS Sierra.

I have a small Python program that calls a shell script in a loop with a 
time.sleep() in it.
The shell script is called with subprocess.Popen(), followed by a 
subprocess.wait().
No information is exchanged with the shell script.

Once in a while I send a SIGINT to the program to cancel the sleep. I don't 
know if the sleep is active at the time of the SIGINT, but most of the time it 
will be as it takes much more time than the shell script.

I do this by having a handler for the SIGINT which does nothing. It is just 
there to have the SIGINT silently cancel the sleep.

Now after weeks of running correctly it crashed. It appears that the SIGINT 
came during the Popen. This causes a system call in the Popen to be terminated 
with EINTR and Popen then retries that system call, but unfortunately it fails. 
It seems to me that this could be a bug.

Here is the traceback:

Traceback (most recent call last):
  File "/Users/piet/bin/smoveinloop.py", line 86, in 
main()
  File "/Users/piet/bin/smoveinloop.py", line 65, in main
task = subprocess.Popen([TASK], executable=TASK)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
 line 390, in __init__
errread, errwrite)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
 line 1001, in _execute_child
data = _eintr_retry_call(os.read, errpipe_read, 1048576)
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
 line 121, in _eintr_retry_call
return func(*args)
OSError: [Errno 22] Invalid argument

TASK is a string with the name of the shell script.

Can anybody shed some light on this?
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]

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


Re: String to Dictionary conversion in python

2017-09-16 Thread Piet van Oostrum
Rustom Mody <rustompm...@gmail.com> writes:

> On Saturday, September 16, 2017 at 2:04:39 AM UTC+5:30, jlad...@itu.edu wrote:

> Yeah… I used to think thus
> But literal_eval has excessive crud in its error messages:
>
>>>> from ast import literal_eval
>
>>>> literal_eval("{'x':1}")
> {'x': 1}
>
> Ok…
>
>>>> literal_eval("{x:1}")
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
> return _convert(node_or_string)
>   File "/usr/lib/python2.7/ast.py", line 63, in _convert
> in zip(node.keys, node.values))
>   File "/usr/lib/python2.7/ast.py", line 62, in 
> return dict((_convert(k), _convert(v)) for k, v
>   File "/usr/lib/python2.7/ast.py", line 79, in _convert
> raise ValueError('malformed string')
> ValueError: malformed string
>

You can catch the exception and print a nice error message:

from ast import literal_eval
def literal(s):
   try:
   return literal_eval('{'+s+'}')
   except Exception as e:
   print "%s: %s" % (type(e).__name__, ', '.join(e.args))

>>> literal("'x':1")
{'x': 1}
>>> literal("x:1")
ValueError: malformed string

But in non-interactive use you probably want to propagate the exception.

-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What extended ASCII character set uses 0x9D?

2017-08-18 Thread Piet van Oostrum
Marko Rauhamaa <ma...@pacujo.net> writes:

> Chris Angelico <ros...@gmail.com>:
>
>> Ohh. We have no evidence that uppercasing is going on here, and a
>> naive ASCII upper-casing wouldn't produce 0x81 either - if it did, it
>> would also convert 0x21 ("!") into 0x01 (SOH, a control character). So
>> this one's still a mystery.
>
> BTW, I was reading up on the history of ASCII control characters. Quite
> fascinating.
>
> For example, have you ever wondered why DEL is the odd control character
> out at the code point 127? The reason turns out to be paper punch tape.
> By backstepping and punching a DEL over the previous ASCII character you
> can "rub out" the character.
>
Sure, I have done that many times. Years ago.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to group by function if one of the group has relationship with another one in the group?

2017-08-01 Thread Piet van Oostrum
Ho Yeung Lee <jobmatt...@gmail.com> writes:

> which function should be used for this problem?
>
I think it is a kind if clustering, or a connectivity problem. There are 
special algorithms for that, not just a simple function. Maybe scikit-learn has 
a suitable algorithm for it.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to sort a list of tuples with custom function

2017-08-01 Thread Piet van Oostrum
Ho Yeung Lee <jobmatt...@gmail.com> writes:

> def isneighborlocation(lo1, lo2):
> if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> return 1
> elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> return 1
> elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> return 1
> else:
> return 0
>
>
> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
>
> return something like
> [(1,2),(3,3),(2,5)]

I think you are trying to sort a list of two-dimensional points into a
one-dimensiqonal list in such a way thet points that are close together
in the two-dimensional sense will also be close together in the
one-dimensional list. But that is impossible.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help to understand not the answer

2017-07-29 Thread Piet van Oostrum
new_to_c0ding <devinderau...@gmail.com> writes:

> Now as per instructions, class MITCampus(Campus) has  (self, center_loc, 
> tent_loc = Location(0,0)) and it is mentioned that center_loc and tent_loc 
> are Location objects but when I code them as Locations, I get error from the 
> tester:
> Traceback (most recent call last):
>   File "submission.py", line 61, in __init__
> self.cloc=Location(center_loc)
> TypeError: __init__() missing 1 required positional argument: 'y'

Location must be called with 2 parameters: a x and a y coordinate, not
with another location as parameter.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-29 Thread Piet van Oostrum
Peter Otten <__pete...@web.de> writes:

> Ho Yeung Lee wrote:
>
>> from itertools import groupby
>> 
>> testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
>> def isneighborlocation(lo1, lo2):
>> if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
>> return 1
>> elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
>> return 1
>> else:
>> return 0
>> 
>> groupda = groupby(testing1, isneighborlocation)
>> for key, group1 in groupda:
>> print key
>> for thing in group1:
>> print thing
>> 
>> expect output 3 group
>> group1 [(1,1)]
>> group2 [(2,3),(2,4]
>> group3 [(3,5),(3,6),(4,6)]
>
> groupby() calculates the key value from the current item only, so there's no 
> "natural" way to apply it to your problem.
>
> Possible workarounds are to feed it pairs of neighbouring items (think 
> zip()) or a stateful key function. Below is an example of the latter:
>
> $ cat sequential_group_class.py
> from itertools import groupby
>
> missing = object()
>
> class PairKey:
> def __init__(self, continued):
> self.prev = missing
> self.continued = continued
> self.key = False
>
> def __call__(self, item):
> if self.prev is not missing and not self.continued(self.prev, item):
> self.key = not self.key
> self.prev = item
> return self.key
>
> def isneighborlocation(lo1, lo2):
> x1, y1 = lo1
> x2, y2 = lo2
> dx = x1 - x2
> dy = y1 - y2
> return dx*dx + dy*dy <= 1
>
> items = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
>
> for key, group in groupby(items, key=PairKey(isneighborlocation)):
> print key, list(group)
>
> $ python sequential_group_class.py 
> False [(1, 1)]
> True [(2, 3), (2, 4)]
> False [(3, 5), (3, 6), (4, 6)]

That only works if
(a) The elements in the list are already clustered on group (i.e. all
elements of a group are adjacent)
(b) In a group the order is such that adjacent elements are direct
neigbours, i.e. their distance is at most 1.

So 'groupby' is not a natural solution for this problem.
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python BeautifulSoup extract html table cells that contains images and text

2017-07-29 Thread Piet van Oostrum
Umar Yusuf <bintacomput...@gmail.com> writes:

> Hi all,
>
> I need help extracting the table from this url...?
>
> from bs4 import BeautifulSoup
> url = "https://www.marinetraffic.com/en/ais/index/ports/all/per_page:50;
>
> headers = {'User-agent': 'Mozilla/5.0'}
> raw_html = requests.get(url, headers=headers)
>
> raw_data = raw_html.text
> soup_data = BeautifulSoup(raw_data, "lxml")
>
> td = soup_data.findAll('tr')[1:]
>
> country = []
>
> for data in td:
> col = data.find_all('td')
>     country.append(col)

So what data do you want to extract?
-- 
Piet van Oostrum <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-04-11 Thread Piet van Oostrum
Vincent Vande Vyvre <vincent.vande.vy...@telenet.be> writes:

> final = [(x, y+1) for x, y in zip(e, e)]
final = [(x, x+1) for x in e]
-- 
Piet van Oostrum <pie...@pietvanoostrum.com>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-04-06 Thread Piet van Oostrum
Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> writes:

> Suppose you have an expensive calculation that gets used two or more times in 
> a 
> loop. The obvious way to avoid calculating it twice in an ordinary loop is 
> with 
> a temporary variable:
>
> result = []
> for x in data:
> tmp = expensive_calculation(x)
> result.append((tmp, tmp+1))
>
>
> But what if you are using a list comprehension? Alas, list comps don't let 
> you 
> have temporary variables, so you have to write this:
>
>
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
>
>
> Or do you? ... no, you don't!
>
>
> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]
>
>
> I can't decide whether that's an awesome trick or a horrible hack...

It is a poor man's 'let'. It would be nice if python had a real 'let'
construction. Or for example:

[(tmp, tmp + 1) for x in data with tmp = expensive_calculation(x)]

Alas!
-- 
Piet van Oostrum <pie...@pietvanoostrum.com>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting stdout and stderr from subprocess in correct order

2017-03-04 Thread Piet van Oostrum
"Ivan \"Rambius\" Ivanov" <rambiusparkisan...@gmail.com> writes:

> Dear colleagues,
>
> I using subprocess module and I am wondering how I can get the output
> of the spawned process's stdout and stderr in the right order. Here
> are my sample programs:
>
> $ cat subprc.py
> import subprocess
> import sys
>
> f = 'hw.py'
> p = subprocess.run([sys.executable, f], stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> print(p.stdout)
> print(p.stderr)
>
> $ cat hw.py
> import sys
>
> print("a", file=sys.stdout)
> print("b", file=sys.stderr)
> print("c", file=sys.stdout)
> print("d", file=sys.stderr)
>
> When i run hw.py I get
>
> $ ./hw.py
> a
> b
> c
> d
>
> When I run it through subprc.py, I do get standard out and standard
> errors streams, but they are separated and not in the order above:
>
> $ ./subprc.py
> b'a\nc\n'
> b'b\nd\n'
>
> How should I use subprocess in order to get the outputs in the correct
> order? Thank you for your help in advance.
>
If you want them in the order they are produced then you have to put
them through a single channel with stderr=subprocess.STDOUT.

p = subprocess.run([sys.executable, f], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, encoding='ascii')

I added an encoding so that it will be read as a text file rather than
binary. And of course you should read only stdout (stderr will give
None)

Moreover you must make sure that each write is flushed. This can be done
in three ways:

1. Add the flush=True argument to the print statements.
print("a", file=sys.stdout, flush=True)
print("b", file=sys.stderr, flush=True)
print("c", file=sys.stdout, flush=True)
print("d", file=sys.stderr, flush=True)

2. Change the files to a line buffered version.
import os
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 1)

3. Add a flush() call after each print statement, like
sys.stdout.flush()

Then you get them in order.
$ python3 subprc.py
a
b
c
d

Of course you won't be able to tell from which stream each line comes.
The streams are byte streams, not message streams. You would have to put
extra information, e.g. a prefix, in your print statements.
-- 
Piet van Oostrum <pie...@pietvanoostrum.com>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Expression can be simplified on list

2016-09-17 Thread Piet van Oostrum
Daiyue Weng <daiyuew...@gmail.com> writes:

> Hi, I found that when I tried to make an equality test on empty like,
>
> if errors == []:
>
> PyCharm always warns me,
>
> Expression can be simplified.
>
> I am wondering what's wrong and how to fix this?
>
It is not wrong, but it can be simplified to just:

if errors:

This is quite usual in Python, but some people prefer the more elaborate form, 
or something like:

if len(errors) == 0:
-- 
Piet van Oostrum <pie...@pietvanoostrum.com>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Something wrong with the PIP, lots of friend have the same problem

2016-08-25 Thread Piet van Oostrum
Wentao Liang <wentao.li...@uq.net.au> writes:

> even though I try to upgrade PIP[cid:beb9e7c5-7d98-4506-ba67-9e67890edec1]
>
> 
> From: Wentao Liang
> Sent: Sunday, 21 August 2016 4:35:31 PM
> To: python-list@python.org
> Subject: Something wrong with the PIP, lots of friend have the same problem
>
>
> [cid:1c20248f-a678-40c0-9563-da4f8a1ef3bd]
> Is it the problem from PIP itself, or I used it in a wrong way? []
>
> Looking forwards to your reply!
>
> Thank you
>
> Wentao Liang

Without more information it is difficult to say. What command did you use and 
what was the result?

[Maybe you tried to include images in your post/mail but they did not come 
through. Just copy/paste the text.]
-- 
Piet van Oostrum <pie...@pietvanoostrum.com>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a function is faster than not calling it?

2015-06-22 Thread Piet van Oostrum
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:

 Why is calling a function faster than bypassing the function object and
 evaluating the code object itself? And not by a little, but by a lot?

 Here I have a file, eval_test.py:

 # === cut ===
 from timeit import Timer

 def func():
 a = 2
 b = 3
 c = 4
 return (a+b)*(a-b)/(a*c + b*c)


 code = func.__code__
 assert func() == eval(code)

 t1 = Timer(eval; func(), setup=from __main__ import func)
 t2 = Timer(eval(code), setup=from __main__ import code)

 # Best of 10 trials.
 print (min(t1.repeat(repeat=10)))
 print (min(t2.repeat(repeat=10)))

 # === cut ===


 Note that both tests include a name lookup for eval, so that as much as
 possible I am comparing the two pieces of code on an equal footing.

They are not on equal footing. The first one only looks up eval, but the second 
actually calls eval. The overhead of calling eval is what makes the difference. 
To get them on equal footing you will have to insert an eval call also in the 
first example.

dummy = compile(0, 'string', eval)

t1 = Timer(eval(dummy); func(), setup=from __main__ import dummy, func)
t2 = Timer(0; eval(code), setup=from __main__ import code)

And then you'll see that t1 is slightly slower than t2.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: singleton ... again

2014-02-13 Thread Piet van Oostrum
Ben Finney ben+pyt...@benfinney.id.au writes:

 Gregory Ewing greg.ew...@canterbury.ac.nz writes:

 Roy Smith wrote:
  It looks to me like he's trying to implement a classic Gang of Four
  singleton pattern.

 Which I've never really seen the point of in Python, or any other
 language for that matter. Just create one instance of the class during
 initialisation, put it in a global somewhere, and use it thereafter.

 Make that “somewhere” a module namespace, and you effectively have a
 Singleton for all practical purposes. So yes, I see the point of it; but
 we already have it built in :-)

There is a use case for a singleton class: when creating the singleton
object takes considerable resources and you don't need it always in your
program.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: A new version (0.3.6) of python-gnupg has been released.

2014-02-06 Thread Piet van Oostrum
Vinay Sajip vinay_sa...@yahoo.co.uk writes:

 A new version of the Python module which wraps GnuPG has been
 released.

There seem to be 2 gnupg modules for Python. The other one has version number 
1.2.5. Very confusing!
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Highlighting program variables instead of keywords?

2014-01-29 Thread Piet van Oostrum
Ned Batchelder n...@nedbatchelder.com writes:

 On 1/28/14 2:19 AM, wxjmfa...@gmail.com wrote:
 Different, but a little bit related. The work
 which is done actually on the possibility (not
 implemented but alreay realized) to colorize (style)
 the different graphemes of a glyph is very interesting.

 Python with its absurd Flexible String Representation
 just become a no go for the kind of task.

 (Should not be too complicate to understand.)

 jmf


 JMF, seriously, stop it.  You've convinced no one because you have no
 convincing arguments.

 It's obnoxious to continue to make this claim.  Stop it.  Please.

 If you want to try to convince someone, convince me.  Write to me
 offline: n...@nedbatchelder.com

 -- 
 Ned Batchelder, http://nedbatchelder.com


I seriously think jmf has a mental disorder. So these reactions won't do 
anything useful. Just ignore.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can post a code but afraid of plagiarism

2014-01-24 Thread Piet van Oostrum
indar kumar indarkuma...@gmail.com writes:

 On Saturday, January 18, 2014 3:21:42 PM UTC-7, indar kumar wrote:
 Hi,
 
 
 
 I want to show a code for review but afraid of plagiarism issues.
 Kindly, suggest how can I post it for review here without masking it
 visible for public

 Can I do the following to just get the value as string not the type list?

 searchfor = '192.168.0.2'
 z=[ii[0] for ii in hosts.values() if ii[1] == searchfor]
 str1 = ''.join(z)
 str1

If you want to extract an element of a list use indexing, like mylist[0]. 

If you don't know these things or can't find this out yourself, you have a 
serious lack of knowledge about Python, or maybe about programming, and it is 
time to learn that first.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Early retirement project?

2014-01-22 Thread Piet van Oostrum
wxjmfa...@gmail.com writes:


 In fact, Python just becomes the last tool I (would)
 recommend, especially for non-ascii users.

 jmf

In fact, Python 3 is one of the best programming tools for non-ASCII users.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to write this as a list comprehension?

2014-01-20 Thread Piet van Oostrum
Rhodri James rho...@wildebst.org.uk writes:

 On Sat, 18 Jan 2014 16:00:45 -, Jussi Piitulainen
 jpiit...@ling.helsinki.fi wrote:

[...]

 I would write that on three lines anyway, properly indented:

   [ somefunc(mn,day,wd,name)
 for (then, name) in mylist
 let (_,mn,dy,_,_,_,wd,_,_) = localtime(then) ]

 It could be made to use existing keywords:

   [ somefunc(mn,day,wd,name)
 for (then, name) in mylist
 with localtime(then) as (_,mn,dy,_,_,_,wd,_,_) ]

 Better, in that it's readable.  It's still storing up trouble, though.

 Seriously, what's inelegant about this?

 def meaningful_name(then, name):
 _,mn,dy,_,_,_,wd,_,_ = localtime(then)
 return somefunc(mn, dy, wd, name)

 ...

 [meaningful_name(then, name) for (then, name) in mylist]

 I assume there's some good reason you didn't want somefunc() to do the
 localtime() itself?

Actually in my use case somefunc(mn,day,wd,name) was an expression, not
really a function call. I just presented it like that to make it a more
generalised abstract case. So in my case that would introduce an
additional function outside of the comprehension.

Let me summarize what my problem really was: In a comprehension you
generally need local name(s), which can only be introduced with the
'for' construction. But then the name(s) must be bound to an element of
a sequence/iterator. There is no way to bind the name(s) to a single
object other than putting that object in a one element sequence. I was
just looking for a way to avoid that. Functional programming languages
have a way to do this with the 'let' or 'where' construction which is
missing in Python.

Thanks everybody for your thoughts on this.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to write this as a list comprehension?

2014-01-19 Thread Piet van Oostrum
John Allsup py...@allsup.co writes:

 Hi,

 I'd agree with the advice that it's not the best idea: readability sucks
 here, but consider the following:


 import time

 def somefunc(a,b,c,d): # dummy function
 return {} - {} - {} : {}.format(a,b,c,d)
 l = [(time.time(),name {}.format(n)) for n in range(100)] # dummy data

 # the line in question
 labels = [somefunc(*(lambda t,n:
 (t.tm_mon,t.tm_mday,t.tm_wday,n))(time.localtime(x[0]),x[1])) for x in
 l]


 print(labels) # just to see the result


 If you find that hard to decipher, the consider the maintainability of
 code you write that uses such comprehensions.  You need to include
 comments that explain what this does, and it is easier to write a
 longhand version using .append() and variable assignments.  I presume
 performance won't be an issue determining the right approach, since then
 you'd be using C or C++.

I'll stay with

labels = [somefunc(mn, day, wd, name) 
for then, name in mylist
for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]

where (sic!) the last part means as much as 
   where _, mn, dy, _, _, _, wd, _, _ = localtime(then)

I find the list comprehension preferable because it makes it more clear
that a new list is constructed from an existing list, something that is
not as immediately clear with the append construction.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to write this as a list comprehension?

2014-01-18 Thread Piet van Oostrum
Rustom Mody rustompm...@gmail.com writes:

 On Saturday, January 18, 2014 4:49:55 AM UTC+5:30, Piet van Oostrum wrote:
[...]

 Python misses a 'where' or 'let'-like construction as in Haskell.

 +1
 Yes Ive often been bitten by the lack of a 'comprehension-let'

If it used only in a comprehension as in my example you can write instead of 
'where vars = expr':
   for vars in [expr], unnecessarily construction a one element list.
If there would be a syntax like:
   for vars = expr
this could be avoided.

 Something like this is possible??


 [somefunc(mn,day,wd,name) for (_, mn,dy,_,_,_,wd,_,_), name) in
 [localtime(then), name for then, name in mylist]]
It works modulo some corrections in the parentheses:

[somefunc(mn,day,wd,name) for (_, mn,dy,_,_,_,wd,_,_), name in
  [(localtime(then), name) for then, name in mylist]]
but I find that hardly more elegant.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to write this as a list comprehension?

2014-01-18 Thread Piet van Oostrum
Alain Ketterlin al...@dpt-info.u-strasbg.fr writes:

 Piet van Oostrum p...@vanoostrum.org writes:
 [...]
 Python misses a 'where' or 'let'-like construction as in Haskell.

 let x = v in e really is (lambda x:e)(v)

You are right, but it is a lot less readable IMHO.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


How to write this as a list comprehension?

2014-01-17 Thread Piet van Oostrum
Hi,

I am looking for an elegant way to write the following code as a list
comprehension:

labels = []
for then, name in mylist:
_, mn, dy, _, _, _, wd, _, _ = localtime(then)
labels.append(somefunc(mn, day, wd, name))

So mylist is a list of tuples, the first member of the tuple is a time
(as epoch offset) and I neeed to apply a function on some fields of the
localtime of it.

I could define a auxiliary function like:

def auxfunc(then, name):
_, mn, dy, _, _, _, wd, _, _ = localtime(then)
return somefunc(mn, day, wd, name)

and then use 
[auxfunc(then, name) for then, name in mylist]

or even
[auxfunc(*tup) for tup in mylist]

But defining the auxfunc takes away the elegance of a list comprehension. I 
would like to integrate the unpacking of localtime() and calling somefunc 
within the list comprehension, but I don't see a simple way to do that.

somefunc(mn, day, wd, name) for _, mn, dy, _, _, _, wd, _, _ in 
[localtime(then)] 
(i.e. using a list comprehension on a one element list to do the variable 
shuffling) 
works but I don't find that very elegant.

labels = [somefunc(mn, day, wd, name) 
for then, name in mylist
for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]

Python misses a 'where' or 'let'-like construction as in Haskell.

Anybody has a more elegant solution?
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.x adoption

2014-01-16 Thread Piet van Oostrum
Travis Griggs travisgri...@gmail.com writes:

 Personally, I wish they’d start python4, sure would take the heat out of
 the 3 vs 2 debates. And maybe there’d be a program called twentyfour as
 a result.

twelve would be sufficient, I would think.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: setup.py issue - some files are included as intended, but one is not

2014-01-15 Thread Piet van Oostrum
Dan Stromberg drsali...@gmail.com writes:

 On Sat, Jan 11, 2014 at 2:04 PM, Dan Stromberg drsali...@gmail.com wrote:
 Hi folks.

 I have a setup.py problem that's driving me nuts.

 Anyone?  I've received 0 responses.

I can't even install your code because there's a bug in it.

m4_treap.m4 contains this instruction twice:

ifdef(/*pyx*/,cp)if current is None:
ifdef(/*pyx*/,cp)raise KeyError

Which when generating pyx_treap.pyx (with *pyx* defined) expands to the 
syntactically incorrect

cpif current is None:
cpraise KeyError

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Monkeypatching a staticmethod?

2014-01-10 Thread Piet van Oostrum
Ian Kelly ian.g.ke...@gmail.com writes:

 I suggest defining x as a normal function and writing the assignment
 as Foo.x = staticmethod(x) to keep x callable from the global
 namespace.  Or just del it after doing the monkey patch.

You can use Foo.x = staticmethod(lambda: 2)
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: the Gravity of Python 2

2014-01-09 Thread Piet van Oostrum
Kushal Kumaran kushal.kuma...@gmail.com writes:

 Yes, but the documentation for utcnow explicitly tells you how to get
 an aware object.

   An aware current UTC datetime can be obtained by calling
datetime.now(timezone.utc).

And in Python 2.7 you can just copy the definition of utc from the doc and use 
that:

from datetime import tzinfo, timedelta, datetime

ZERO = timedelta(0)

class UTC(tzinfo):
UTC

def utcoffset(self, dt):
return ZERO

def tzname(self, dt):
return UTC

def dst(self, dt):
return ZERO

utc = UTC()
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: the Gravity of Python 2

2014-01-09 Thread Piet van Oostrum
Chris Angelico ros...@gmail.com writes:

 On Thu, Jan 9, 2014 at 2:34 PM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 [ a bunch of stuff that I totally agree with ]

 No response needed here :)

 So I was wrong on the specific example of .today(), but asking the
 question the other way is at least helpful. Maybe the best solution is
 exactly what Roy already posted, or maybe there's some other way to
 achieve that. In any case, there is a solution, albeit not as clean as
 I would have liked.

 With time zones, as with text encodings, there is a single technically
 elegant solution (for text: Unicode; for time zones: twelve simple,
 static zones that never change)

 Twelve or twenty-four? Or are you thinking we should all be an even
 number of hours away from UTC, which would also work?

Even 24 doesn't take into account DST.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: the Gravity of Python 2

2014-01-09 Thread Piet van Oostrum
Chris Angelico ros...@gmail.com writes:

 On Fri, Jan 10, 2014 at 1:06 AM, Piet van Oostrum p...@vanoostrum.org wrote:
 Chris Angelico ros...@gmail.com writes:

 On Thu, Jan 9, 2014 at 2:34 PM, Ben Finney ben+pyt...@benfinney.id.au 
 wrote:
 With time zones, as with text encodings, there is a single technically
 elegant solution (for text: Unicode; for time zones: twelve simple,
 static zones that never change)

 Twelve or twenty-four? Or are you thinking we should all be an even
 number of hours away from UTC, which would also work?

 Even 24 doesn't take into account DST.

 That was the point. We can abolish codepages by using Unicode, which
 covers everything. We could abolish time messes by having every
 locality settle permanently on one timezone; Ben's theory demands that
 all timezones be some integer number of hours +/- UTC, which IMO is
 optional, but mainly it should be easy to figure out any two
 locations' difference: just subtract one's UTC offset from the
 other's. Similarly, you can calculate the difference between two times
 at the same location by simple subtraction; currently, you also have
 to consider the possibility of a DST switch (from noon to noon across
 a switch is either 23 or 25 hours).

 Actually, the nearest parallel to Unicode is probably use UTC
 everywhere, which makes for a superb internal representation and
 transmission format, but bugs most human beings :)

I don't know how other countries do it, but here, when the clock goes back, it 
goes from 03:00 to 02:00. So I wonder how they communicate when your plane 
leaves at 02:30 in that night. Which 02:30? In that case using UTC may come out 
handy, if it would be understood. Or do the planes just stop leaving during 
that interval? Not that there will be many leaving during that time in general, 
I presume.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bytes indexing returns an int

2014-01-09 Thread Piet van Oostrum
Ned Batchelder n...@nedbatchelder.com writes:

 On 1/8/14 11:08 AM, wxjmfa...@gmail.com wrote:
 Byte strings (encoded code points) or native unicode is one
 thing.

 But on the other side, the problem is elsewhere. These very
 talented ascii narrow minded, unicode illiterate devs only
 succeded to produce this (I, really, do not wish to be rude).

 If you don't want to be rude, you are failing.  You've been told a
 number of times that your obscure micro-benchmarks are meaningless.  Now
 you've taken to calling the core devs narrow-minded and Unicode
 illiterate.  They are neither of these things.

 Continuing to post these comments with no interest in learning is rude.
 Other recent threads have contained details rebuttals of your views,
 which you have ignored.  This is rude. Please stop.

Please ignore jmf's repeated nonsense.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python/Django Extract and append only new links

2014-01-01 Thread Piet van Oostrum
Max Cuban edze...@gmail.com writes:

 I am putting together a project using Python 2.7 Django 1.5 on Windows 7.
 I believe this should be on the django group but I haven't had help
 from there so I figured I would try the python list
 I have the following view:
 views.py:

[snip]

 Right now as my code stands, anytime I run it, it scraps all the links
 on the frontpage of the sites selected and presents them paginated all
 afresh. However, I don't think its a good idea for the script to
 read/write all the links that had previously extracted links all over
 again and therefore would like to check for and append only new links.
 I would like to save the previously scraped links so that over the
 course of say, a week, all the links that have appeared on the
 frontpage of these sites will be available on my site as older pages.

 It's my first programming project and don't know how to incorporate
 this logic into my code.

 Any help/pointers/references will be greatly appreciated.

 regards, Max

I don't know anything about Django, but I don't think this is a Django
question.

I think the best way would be to put the urls in a database with the
time that they have been retrieved. Then you could retrieve the links
from the database next time, and when present, sort them on time
retrieved and put them at the end of the list.

Now if you want to do this on a user basis you should add user
information with it also (and then it would be partly a Django problem
because you would get the user id from Django).

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python mange with liste

2013-12-29 Thread Piet van Oostrum
Bala Ji bala...@gmail.com writes:

 Hello guys,
 i need some help with is program

 I have a txt file test.txt where there is Name;Sexe;Answer(Y or N)
 example of txt file:
 --
 nam1;F;Y
 nam2;M;N
 nam3;F;Y
 nam4;M;N
 halo;M;Y
 rock;M;N
 nam1;F;N
 _

 so my program will ask the name, sexe, and answer and it will tell me if the 
 name exist or not

 example i will enter
 nam1;F;O

What does the O mean here?

 The program must tell me that nam1 with sexe F existe. (it must take in count 
 the answer)

 
 name = raw_input('name: ')
 sexe = raw_input('sexe: ')
 r1 = raw_input('r1 Y or N: ')
 infos = name+;+sexe+;+r1

 f=open(test.txt,r)
 conten = f.read()
 print conten
 f.close()

   #f=open(test.txt,a)
   #f.write(infos)
   #f.write('\n')
   #f.close()
 

 thank you =)

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python socket query

2013-12-23 Thread Piet van Oostrum
smilesonisa...@gmail.com writes:

 Hi,
I am trying to write a TCP socket program in python. I am using python 2.6 
 in linux. 

 I referred following link:
 http://www.ibm.com/developerworks/linux/tutorials/l-pysocks/section4.html
 I am actually writing the client-side stream socket.
 I wrote a small program which creates the socket, bind to the socket, connect 
 to socket and send() close(). I see that there is no reply coming from server 
 and the TCP disconnect happens.
 import socket

 def tcp(host, request, port=34567):

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

 s.connect((host, port))

 s.send(request)

 reply = s.recv(2**14)

 s.close()

 return reply

 My problem is even if the request is sent the length(reply) is is 0. I tried 
 to put a timeout of 1 sec s.settimeout() call after the send call but it 
 doesnot help.

 I tried by commenting s.close() still it did not work.

 Any idea what is the problem?

Length(reply) == 0 means that the other side closed the socket without sending 
anything back.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Oh look, another language (ceylon)

2013-11-18 Thread Piet van Oostrum
Chris Angelico ros...@gmail.com writes:

 On Mon, Nov 18, 2013 at 11:29 PM, Ian Kelly ian.g.ke...@gmail.com wrote:

 On Nov 18, 2013 3:06 AM, Chris Angelico ros...@gmail.com wrote:

 I'm trying to figure this out. Reading the docs hasn't answered this.
 If each character in a string is a 32-bit Unicode character, and (as
 can be seen in the examples) string indexing and slicing are
 supported, then does string indexing mean counting from the beginning
 to see if there were any surrogate pairs?

 The string reference says:

 Since a String has an underlying UTF-16 encoding, certain operations are
 expensive, requiring iteration of the characters of the string. In
 particular, size requires iteration of the whole string, and get(), span(),
 and segment() require iteration from the beginning of the string to the
 given index.

 The get and span operations appear to be equivalent to indexing and slicing.

 Right, that's what I was looking for and didn't find. (I was searching
 the one-page reference manual rather than reading in detail.) So, yes,
 they're O(n) operations. Thanks for hunting that down.

 ChrisA

It would be so much better to use the Flexible String Representation.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: If you continue being rude i will continue doing this

2013-11-18 Thread Piet van Oostrum
Ferrous Cranus nikos.gr...@gmail.com writes:

 No i haven't broke it at all.
 Everything work as they should.

 The refusal of 'pygeoip' to install turned out to be the local setting in my 
 new VPS.

 So i have changes it to:

 export LANG = en_US.UTF-8

 and then 'pip install pygeoip' was successful. 

 Trying to figure out how to install-setup EPEL repository along with
 python3  python3-pip and 2 extra modules my script needed in my new
 VPS have costed 4-5 of my life and of my mental health, while if you
 just helped a bit these would have been done in a couple of hours.

How could anyone have known that this was the problem? AFIAK you didn't even 
tell about the VPS. And moreover this wasn't a Python problem, so off topic 
here.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: splitting file/content into lines based on regex termination

2013-11-09 Thread Piet van Oostrum
bruce badoug...@gmail.com writes:

 hi.

 thanks for the reply.

 tried what you suggested. what I see now, is that I print out the
 lines, but not the regex data at all. my initial try, gave me the
 line, and then the next items , followed by the next line, etc...

exp = re.compile(r(br#\d+\s*/\s*\d+#\d+#))

exp.split(s)
=
['10116#000#C S#S#100##001##DAY#Fund of 
Computing#Barrett,\nWilliam#3#MWFbr#08:00ambr#08:50ambr#3718 HBLL ',
 'br#45 /\n58#0#',
 '10116#000#C S#S#100##002##DAY#Fund of 
Computing#Barrett,\nWilliam#3#MWFbr#09:00ambr#09:50ambr#3718 HBLL ',
 'br#9 /\n58#0#',
 '10178#000#C S#S#124##001##DAY#Computer 
Systems#Roper,\nPaul#3#MWFbr#11:00ambr#11:50ambr#1170 TMCB ',
 'br#41 /\n145#0#',
 '10178#000#C S#S#124##002##DAY#Computer 
Systems#Roper,\nPaul#3#MWFbr#2:00pmbr#2:50pmbr#1170 TMCB ',
 'br#40 /\n120#0#',
 01489#002#C S#S#142##001##DAY#Intro to Computer\nProgramming#Burton, Robert 
div class='instructors'Seppi, Kevinbr\n//div]
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Adding 'download' column to existing 'visitors' table (as requested)

2013-11-08 Thread Piet van Oostrum
Sibylle Koczian nulla.epist...@web.de writes:

 Am 07.11.2013 14:14, schrieb Piet van Oostrum:
 Nick the Gr33knikos.gr...@gmail.com  writes:

 I have decided to take your advice.
 I wasn't able to fit those 'lists' of mine into MySQL's varchar()
 datatype after converting them to long strings and that sads me.

 My implementation is like the following.
 I do not use an extra table of downlaods that i asoociate with table
 visitors with a foreing key but decided to add an additional 'download'
 column into the existant visitors table:

 Nikos, you are an excellent member of the Greek society. Listening to you 
 makes it so much easier to understand the problems that your country has.

 Is there any reason at all to insult all other Greek readers of this
 newsgroup?

I was talking about the Greek nation. That doesn't imply that every single 
Greek is like that.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code

2013-11-07 Thread Piet van Oostrum
chovd...@gmail.com writes:


 Hi friends

 help me with the following code. Im able to execute the code but getting 
 wrong output

 def sequence_b(N):
 N = 10
 result = 0
 for k in xrange (1,N):
 result  +=  ((-1) ** (k+1))/2*k-1
 print result
 print sequence_b(10)

 This is the output which im getting
 -1
 -4
 -5
 -10
 -11
 -18
 -19
 -28
 -29


 But i want output as
 1
 -1/3
 1/5
 -1/7
 1/9
 -1/11
 1/13
 -1/15
 1/17
 -1/19

You probably want this:

N = 10
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
result += step
print(step)

Note: 
1. You don't use the parameter N, you immediately change it to 10. Leave the 
line N = 10 out.
2. Your function doesn't return its result, so it returns None. So the print 
sequence_b(10) dosn't make sense.
If the print is only for debugging the use the following:

def sequence_b(N):
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
print(step) ## debug output
result += step
return result

print(sequence_b(10)) # print the result of the function call

[I use print() because I use Python 3]
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Adding 'download' column to existing 'visitors' table (as requested)

2013-11-07 Thread Piet van Oostrum
Nick the Gr33k nikos.gr...@gmail.com writes:

 I have decided to take your advice.
 I wasn't able to fit those 'lists' of mine into MySQL's varchar()
 datatype after converting them to long strings and that sads me.

 My implementation is like the following.
 I do not use an extra table of downlaods that i asoociate with table
 visitors with a foreing key but decided to add an additional 'download'
 column into the existant visitors table:

Nikos, you are an excellent member of the Greek society. Listening to you makes 
it so much easier to understand the problems that your country has.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Piet van Oostrum
Nick the Gr33k nikos.gr...@gmail.com writes:


 IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.

 ALL I WANT IT TO DO IS JUST

 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
 2. CONVERT LONG STRINGS TO LISTS
 3. ADD SOME CURRENT VALUES TO THOSE LISTS
 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
 PYTHON DATATYPE TO MYSQL SCALAR STRING.

 EVERYHTIGN I TRIED FAILED.

Then why don't you use the simple solution: use a relational database to store 
the data?
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


[issue19490] Problem installing matplotlib 1.3.1 with Python 2.7.6rc1 and 3.3.3rc1

2013-11-05 Thread Piet van Oostrum

Piet van Oostrum added the comment:

I think future versions of Python should add the relevant information about how 
they are linked to Tcl/Tk in sysconfig. This would include the path of the 
include files, the shared libraries and the tcl files. Or a framework location 
on OS X if this is used. The setup.py for extensions that need to link to 
Tcl/Tk can then interrogate this information, and fall back to the current way, 
if it is not available.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19490
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Problem installing matplotlib 1.3.1 with Python 2.7.6 and 3.3.3 (release candidate 1)

2013-11-03 Thread Piet van Oostrum
Hello,

I tried to install matplotlib 1.3.1 on the release candidates of Python 2.7.6 
and 3.3.3.
I am on Mac OS X 10.6.8.
Although the installation gave no problems, there is a problem with Tcl/Tk.

The new Pythons have their own embedded Tcl/Tk, but when installing matplotlib 
it links to the Frameworks version of Tcl and TK, not to the embedded version. 
This causes confusion when importing matplotlib.pyplot:

objc[70648]: Class TKApplication is implemented in both 
/Library/Frameworks/Python.framework/Versions/2.7/lib/libtk8.5.dylib and 
/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. 
Which one is undefined.
objc[70648]: Class TKMenu is implemented in both 
/Library/Frameworks/Python.framework/Versions/2.7/lib/libtk8.5.dylib and 
/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. 
Which one is undefined.
objc[70648]: Class TKContentView is implemented in both 
/Library/Frameworks/Python.framework/Versions/2.7/lib/libtk8.5.dylib and 
/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. 
Which one is undefined.
objc[70648]: Class TKWindow is implemented in both 
/Library/Frameworks/Python.framework/Versions/2.7/lib/libtk8.5.dylib and 
/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. 
Which one is undefined.

And then later it gives a lot of error messages.

So I think it should be linked to the embedded version. For this the matplotlib 
setupext.py should be adapted to find out if there is an embedded Tcl/Tk in the 
Python installation and set the link parameters accordingly. However, the 
installed Python versions (from the DMG's) do not contain the Tcl/Tk header 
files, only the shared library and the tcl files. So I thing the distributed 
Python should also include the Tcl/Tk header files.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: trying to strip out non ascii.. or rather convert non ascii

2013-10-29 Thread Piet van Oostrum
Mark Lawrence breamore...@yahoo.co.uk writes:

 Please provide hard evidence to support your claims or stop posting this
 ridiculous nonsense.  Give us real world problems that can be reported
 on the bug tracker, investigated and resolved.

I think it is much better just to ignore this nonsense instead of asking for 
evidence you know you will never get.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to avoid checking the same condition repeatedly ?

2013-10-28 Thread Piet van Oostrum
Wolfgang Maier wolfgang.ma...@biologie.uni-freiburg.de writes:

 Dear all,
 this is a recurring programming problem that I'm just not sure how to solve
 optimally, so I thought I'd ask for your advice:
 imagine you have a flag set somewhere earlier in your code, e.g.,

 needs_processing = True

 then in a for loop you're processing the elements of an iterable, but the
 kind of processing depends on the flag, e.g.,:

 for elem in iterable:
 if needs_processing:
 pre_process(elem)  # reformat elem in place
 print(elem)

 this checks the condition every time through the for loop, even though there
 is no chance for needs_processing to change inside the loop, which does not
 look very efficient. 

I bet in most cases you won't notice the time used to check the condition.
Beware of premature optimization!
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookie aint retrieving when visiting happens from a backlink.

2013-10-26 Thread Piet van Oostrum
Νίκος Αλεξόπουλος nikos.gr...@gmail.com writes:

 There is no set of cookie returned back when visitor comes from a referer.

 Isn't this strange?
 No matter if you visit a webpage as a direct hit or via a referer the
 cookie on the visitor's browser should have been present.

 But it can only can be found and retrieved as a direct hit and _not_
 from a referrer backlink.

What is happening is that the page comes from the browser cache when you do a 
back link (back button in your browser). So the request doesn't go to your 
server. I checked this by switching off my internet connection and then trying 
both the back button and entering the URL in the address bar. So it has nothing 
to do with referrer, I think.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookie aint retrieving when visiting happens from a backlink.

2013-10-26 Thread Piet van Oostrum
Piet van Oostrum p...@vanoostrum.org writes:

 Νίκος Αλεξόπουλος nikos.gr...@gmail.com writes:

 There is no set of cookie returned back when visitor comes from a referer.

 Isn't this strange?
 No matter if you visit a webpage as a direct hit or via a referer the
 cookie on the visitor's browser should have been present.

 But it can only can be found and retrieved as a direct hit and _not_
 from a referrer backlink.

 What is happening is that the page comes from the browser cache when
 you do a back link (back button in your browser). So the request
 doesn't go to your server. I checked this by switching off my internet
 connection and then trying both the back button and entering the URL
 in the address bar. So it has nothing to do with referrer, I think.

Upon reading another time your message, I now see that you probably mean
clicking on the link about WEB HOSTING in the pages mentioned. I tried
that and in both cases an entry comes in your data base. The only
difference is that the column Αναφέρων contains Direct link' instead of
the referrer website.

So I think the Python code is working and your problem lies somewhere
else, outside the Python world.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Front-end to GCC

2013-10-22 Thread Piet van Oostrum
Mark Janssen dreamingforw...@gmail.com writes:

 I love it.  Watch this...

 [context]
 A language specification in BNF is just syntax. It doesn't say anything
 about semantics. So how could this be used to produce executable C code
 for a program? BNF is used to produce parsers. But a parser isn't
 sufficient.

 A C program is just syntax also.  How does the compiler generate
 executable machine code?  Extrapolate into a Python front-end to C.

 [Dave Angel responds:]
 Did you even read the paragraph you quoted above?  The BNF specification
 does NOT completely describe a language, it only defines its syntax.

 [Steven D'Aprano responds:]
 Like every other language, C programs are certainly not *just* syntax.
 Here is some syntax:

 foo bar^ :=

 Now, I don't know where y'all were taught Computer Science, but BNF
 specifies not only syntax (which would be the *tokens* of a language),
 but also its *grammar*;  how syntax relates to linguistic categories
 like keywords, and tokens relate to each other.

Syntax is grammar. Tokens are part of the grammar (but often specified 
separately with a different grammar, usually regular expressions, which is a 
subset of BNF).

So are you just confused or are you trollong?

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Front-end to GCC

2013-10-22 Thread Piet van Oostrum
Neil Cerutti ne...@norwich.edu writes:


 Context-sensitive grammars can be parse, too.

That's not English. Do you mean parsed? 

But context-sentitive grammars cannot be specified by BNF.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Front-end to GCC

2013-10-22 Thread Piet van Oostrum
Mark Janssen dreamingforw...@gmail.com writes:

 Is your language Turing complete?


 1) No, it's not.
 2) So what?  That should make it easier to compile to C, if anything.
 3) Don't change the subject.

 Well, if your language is not Turing complete, it is not clear that
 you will be able to compile it at all.  That's the difference between
 a calculator and a computer.

You think a language that is not Turing-complete cannot be compiled?
What nonsense is that. Please Mark, spare us your nonsense.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: functools and objective usage

2013-10-22 Thread Piet van Oostrum
Mohsen Pahlevanzadeh moh...@pahlevanzadeh.org writes:

 Dear all,

 Suppose i have function name, 3 arguments for it, and object of its
 caller such as self.blahbalah
 So:
 my function is:
 self.blahblah.name(arg1,arg2,arg3)

 I read functools documentations, may be objictive usage and
 functionality differ, Do you have experience with objective usage ? 
 http://docs.python.org/2/library/functools.html#partial-objects

What do you mean with objective usage?
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Front-end to GCC

2013-10-21 Thread Piet van Oostrum
Mark Janssen dreamingforw...@gmail.com writes:

 No its not like those 'compilers' i dont really agree with a compiler 
 generating C/C++ and saying its producing native code. I dont really believe 
 its truely within the statement. Compilers that do that tend to put in alot 
 of type saftey code and debugging internals at a high level to get things 
 working in other projects i am not saying python compilers here i havent 
 analysed enough to say this.

 Hmm, well what I'd personally find interesting from a computer science
 point of view is a app that will take a language specification in BNF
 (complete with keywords and all) and output C code which is then
 compiled to an executable as normal.  This is how a front-end should
 be designed.  A middle-layer for translating common language elements
 like lists, sets, etc, could make it easy.

A language specification in BNF is just syntax. It doesn't say anything
about semantics. So how could this be used to produce executable C code
for a program? BNF is used to produce parsers. But a parser isn't
sufficient.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: matplotlib 1.3.1

2013-10-19 Thread Piet van Oostrum
Ned Deily n...@acm.org writes:

 In article m2zjq6mugp@cochabamba.vanoostrum.org,
  Piet van Oostrum p...@vanoostrum.org wrote:
 I tried to install it from source, on Mac OS X 10.6.8, with Python
 3.3.2, and Tck/Tk 8.5 installed as Frameworks, but I get an error during
 compilation. It seems it doesn't find the Tcl/TK framework.
 
 
 building 'matplotlib.backends._tkagg' extension
 gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot 
 /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot 
 /Developer/SDKs/MacOSX10.6.sdk -I/opt/local/include 
 -DPY_ARRAY_UNIQUE_SYMBOL=MPL_matplotlib_backends__tkagg_ARRAY_API 
 -DPYCXX_ISO_CPP_LIB=1 -DPYCXX_PYTHON_2TO3=1 
 -I/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-package
 s/numpy/core/include -I/usr/local/include -I/usr/include -I/usr/X11/include 
 -I/opt/local/include -I. -I/Library/Frameworks/Tcl.framework/Headers 
 -I/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders 
 -I/Library/Frameworks/Tk.framework/Headers 
 -I/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders 
 -Iagg24/include 
 -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c 
 src/ag
  g_py_transforms.cpp -o 
  build/temp.macosx-10.6-intel-3.3/src/agg_py_transforms.o -framework Tcl 
  -framework Tk
 In file included from 
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
 numpy/core/include/numpy/ndarraytypes.h:1760,
  from 
  
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
  
 .3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
  from 
  
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
  .3/site-packages/numpy/core/include/numpy/arrayobject.h:4,
  from src/agg_py_transforms.cpp:6:
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
 numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning 
 Using deprecated NumPy API, disable it by  #defining 
 NPY_NO_DEPRECATED_API 
 NPY_1_7_API_VERSION
 i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
 linking not done
 i686-apple-darwin10-gcc-4.2.1: Tcl: linker input file unused because linking 
 not done
 i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
 linking not done
 i686-apple-darwin10-gcc-4.2.1: Tk: linker input file unused because linking 
 not done
 In file included from 
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
 numpy/core/include/numpy/ndarraytypes.h:1760,
  from 
  
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
  
 .3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
  from 
  
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
  .3/site-packages/numpy/core/include/numpy/arrayobject.h:4,
  from src/agg_py_transforms.cpp:6:
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
 numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning 
 Using deprecated NumPy API, disable it by  #defining 
 NPY_NO_DEPRECATED_API 
 NPY_1_7_API_VERSION
 i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
 linking not done
 i686-apple-darwin10-gcc-4.2.1: Tcl: linker input file unused because linking 
 not done
 i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
 linking not done
 i686-apple-darwin10-gcc-4.2.1: Tk: linker input file unused because linking 
 not done
 gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot 
 /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot 
 /Developer/SDKs/MacOSX10.6.sdk -I/opt/local/include 
 -DPY_ARRAY_UNIQUE_SYMBOL=MPL_matplotlib_backends__tkagg_ARRAY_API 
 -DPYCXX_ISO_CPP_LIB=1 -DPYCXX_PYTHON_2TO3=1 
 -I/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-package
 s/numpy/core/include -I/usr/local/include -I/usr/include -I/usr/X11/include 
 -I/opt/local/include -I. -I/Library/Frameworks/Tcl.framework/Headers 
 -I/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders 
 -I/Library/Frameworks/Tk.framework/Headers 
 -I/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders 
 -Iagg24/include 
 -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c 
 src/_t
  kagg.cpp -o build/temp.macosx-10.6-intel-3.3/src/_tkagg.o -framework Tcl 
  -framework Tk
 In file included from 
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
 numpy/core/include/numpy/ndarraytypes.h:1760,
  from 
  
 /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
  
 .3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17

Re: ANN: matplotlib 1.3.1

2013-10-18 Thread Piet van Oostrum
/include/numpy/arrayobject.h:4,
 from src/agg_py_path_iterator.h:7,
 from src/_backend_agg.h:43,
 from src/_tkagg.cpp:20:
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2:
 warning: #warning Using deprecated NumPy API, disable it by  #defining 
NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
In file included from src/_tkagg.cpp:30:
/usr/include/tk.h:23:3: error: #error Tk 8.5 must be compiled with tcl.h from 
Tcl 8.5
In file included from 
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/include/numpy/ndarraytypes.h:1760,
 from 
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
 from 
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/include/numpy/arrayobject.h:4,
 from src/agg_py_path_iterator.h:7,
 from src/_backend_agg.h:43,
 from src/_tkagg.cpp:20:
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2:
 warning: #warning Using deprecated NumPy API, disable it by  #defining 
NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
In file included from src/_tkagg.cpp:30:
/usr/include/tk.h:23:3: error: #error Tk 8.5 must be compiled with tcl.h from 
Tcl 8.5
lipo: can't figure out the architecture type of: 
/var/folders/5r/5r4ywY4e2ReN7U+8ZPQSJTI/-Tmp-//ccXN6p4b.out
error: command 'gcc-4.2' failed with exit status 1
You have new mail in /var/mail/piet
bash-3.2$ hg ftp -su
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python was designed (was Re: Multi-threading in Python vs Java)

2013-10-17 Thread Piet van Oostrum
Peter Cacioppi peter.cacio...@gmail.com writes:

 What you've said here is that without polymorphism, you can't have
 polymorphism. :) 

 Respectfully, no. I refer to the distinction between object based and object 
 oriented programming. Wikipedia's entry is consistent with my understanding 
 (not to argue by wiki-authority, but the terminology here isn't my personal 
 invention).

 Your example of polymorphism in a non OO language makes my tired head hurt. 
 Do you have a clean little example of polymorphism being mocked in a 
 reasonable way with pure C? There are many nice object-based C projects 
 floating around, but real polymorphism? I think you can't do it without some 
 bizarre work-arounds, but I'd be happy to be shown otherwise.

The first C++ compilers were just preprocessors that translated into pure C 
code, which was then compiled with a C compiler. The resulting intermediate C 
code would be an object-oriented program in C. IIRC, the C code was reasonably 
clear, not really convoluted, so you would have been able to write it yourself.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Markers on a matplotlib plot

2013-10-17 Thread Piet van Oostrum
Brandon La Porte laporte.bran...@gmail.com writes:

 I have the following code to make a plot of 4 different supply curves 
 (economics).


 from matplotlib import pyplot as plt

 price = range(0,51)
 q1 = [x/2.0 for x in price]
 q2 = [x/4.0 for x in price]
 q3 = [x/5.0 for x in price]
 q4 = [x/10.0 for x in price]

 markers_on = [20, 40]

 plt.plot(q1,price,'b',q2,price,'g',q3,price,'r', q4, price, 'y' )
 plt.title('Supply Curve')
 plt.xlabel('Quantity Supplied (Thousands per month')
 plt.ylabel('Price ($)')
 #plt.legend(('Kd = %d'%kd, 'Kd = %d'%kd2, 'Kd = %d'% kd3, 'Step'), loc=4)
 plt.legend(('p = 2Qs', 'p = 4Qs', 'p = 5Qs', 'p = 10Qs'), loc=4)

 plt.grid()
 plt.show()

 I would like to place markers on the 4 curves when the price is equal to $20 
 label it A, and when the price is equal to $40 and label it B.  Does anyone 
 know how I can accomplish this.

Something like:

plt.plot(20,40, 'bo')
plt.annotate('B', (20,40), xytext=(-10,10), textcoords='offset points')

Of course you should write a loop to calculate the x, y points, and use the 
proper colors.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib2 timeout issue

2013-10-16 Thread Piet van Oostrum
Jérôme jer...@jolimont.fr writes:

 Hi all.

 I'm having troubles with urllib2 timeout.

 See the following script :

 
 import urllib2
 result = urllib2.urlopen(http://dumdgdfgdgmyurl.com/;)
 print result.readline()
 

 If run on my Debian Wheezy computer, or on my Debian Squeeze server,
 the answer is instantaneous :

 [...]
 urllib2.URLError: urlopen error [Errno -2] Name or service not known

 When run on my Raspberry Pi with Raspian Wheezy, the answer is
 identical but it takes 10 seconds.

 I tried 

 result = urllib2.urlopen(http://dumdgdfgdgmyurl.com/;, timeout=5)

 but I get the same results : instantaneous on Debian, 10 secondes on
 RPi.

 I also added this, as suggested on some StackOverflow pages :

 import socket
 socket.setdefaulttimeout(5)

 and it didn't make any difference.

 In both cases, Python version is Python 2.7.3.

 Am I missing something ?


I would guess the difference to be caused by DNS. Maybe the DNS configuration 
on the RPi is not optimal.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: converting letters to numbers

2013-10-16 Thread Piet van Oostrum
Charles Hixson charleshi...@earthlink.net writes:

 On 10/13/2013 10:02 PM, Steven D'Aprano wrote:
 On Sun, 13 Oct 2013 20:13:32 -0700, Tim Roberts wrote:

 def add(c1, c2):
   % Decode
   c1 = ord(c1) - 65
   c2 = ord(c2) - 65
   % Process
   i1 = (c1 + c2) % 26
   % Encode
   return chr(i1+65)
 Python uses # for comments, not %, as I'm sure you know. What language
 were you thinking off when you wrote the above?



 IIRC Lisp uses % for comments, but it may need to be doubled.  (It's
 been doubled in the examples I've seen, and I don't remember the
 syntax.)
 Perhaps Scheme has the same convention, but Scheme could be considered a
 part of the Lisp clade.

Lisp and scheme use semicolon (;). It wouldn't have been that difficult
to look that up I think.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python was designed

2013-10-16 Thread Piet van Oostrum
Mark Janssen dreamingforw...@gmail.com writes:

 And your earlier idea that punched cards didn't have tokens is wildly
 ignorant of the state of software and languages 50 years ago.

 Please tell me how you parsed tokens with binary switches 50 years
 ago.  Your input is rubbish.

With all due respect, Mark, your remarks are rubbish. Nobody talked about 
parsing input with binary switches except you. I answered that 40 years ago I 
wrote a parser generator that generated a parser for Algol 68 (and another one 
for Algol 60 I should have added). And all this was using punched cards.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python was designed

2013-10-15 Thread Piet van Oostrum
Mark Janssen dreamingforw...@gmail.com writes:

 Yeah, well 40 years ago they didn't have parsers.   The purpose of
 having a field of computer science worthy of the name, is to advance
 the science not let this riff-raff dominate the practice.

Hah! 40 years ago I wrote a parser generator (similar to yacc, that I did not 
know) and used it to generate a parser for Algol 68.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   >