little help in homework required

2017-06-16 Thread JAIDIP patel
I just wrote the code with Python 3.6.1, I am just a beginner in python.
while compiling having below error, can anyone help me in this

TypeError: a bytes-like object is required, not 'str'


The code I wrote:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
mysock.send('GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')
while True:
data =x.recv(512)
if ( len(data) < 1 ) :
break
print (data)

mysock.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: little help in homework required

2017-06-16 Thread Lutz Horn

Hi,


TypeError: a bytes-like object is required, not 'str'

...

mysock.send('GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')


Here you must encode the str as bytes:

mysock.send('GET http://data.pr4e.org/intro-short.txt 
HTTP/1.0\n\n'.encode("UTF-8"))


The actual encoding does not matter in your case since you only have 
ASCII.



data =x.recv(512)


This gives the next error:

NameError: name 'x' is not defined

What is x supposed to be?

Regards

Lutz


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


Re: little help in homework required

2017-06-16 Thread Chris Angelico
On Fri, Jun 16, 2017 at 7:06 PM, JAIDIP patel  wrote:
> The code I wrote:
>
> import socket
>
> mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> mysock.connect(('data.pr4e.org', 80))
> mysock.send('GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')
> while True:
> data =x.recv(512)
> if ( len(data) < 1 ) :
> break
> print (data)
>
> mysock.close()

Lutz has already suggested one solution, but here's an alternative:
you can use a byte-string literal.

mysock.send(b'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n')

While you're at it, though, I recommend complying with protocol correctly:

mysock.send(b'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n')

The HTTP specification says that lines should end with CR LF.

You can actually simplify your condition, too:

if not data:

This is considered more "Pythonic" - that is to say, it's more the way
that experienced Python programmers work. Consider it points for
style. :)

Hope that helps!

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


Re: [OT] is JSON all that great? - was Re: API Help

2017-06-16 Thread alister
On Fri, 16 Jun 2017 00:10:58 +1000, Chris Angelico wrote:

> On Fri, Jun 16, 2017 at 12:00 AM, alister 
> wrote:
>> On Thu, 15 Jun 2017 22:27:40 +1000, Chris Angelico wrote:
>>
>>> On Thu, Jun 15, 2017 at 9:47 PM, Rhodri James 
>>> wrote:
> 1) It is not secure. Check this out:
> https://stackoverflow.com/questions/1906927/xml-
>> vulnerabilities#1907500
 XML and JSON share the vulnerabilities that come from having to parse
 untrusted external input.  XML then has some extra since it has extra
 flexibility, like being able to specify external resources (potential
 attack vectors) or entity substitution.  If you don't need the extra
 flexibility, feel free to use JSON, but don't for one moment think
 that makes you inherently safe.
>>>
>>> Not sure what you mean about parsing untrusted external input. Suppose
>>> you build a web server that receives POST data formatted either JSON
>>> or XML. You take a puddle of bytes, and then proceed to decode them.
>>
>> Where it "Could" be a security issue is in Javascript.
>>
>> Json is designed to be legal Javascript code & therefore directly
>> executable so no parser is posible.
>>
>>
> "no parser is possible"???
> 
> https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/
Global_Objects/JSON/parse
> 
> If you're stupid enough to eval JSON instead of using JSON.parse(),
> you deserve all you get. That's not a fault with JSON.
> 
> ChrisA

i meant possible to use without a parser , sorry



-- 
Dijkstra probably hates me
(Linus Torvalds, in kernel/sched.c)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] is JSON all that great? - was Re: API Help

2017-06-16 Thread alister
On Thu, 15 Jun 2017 21:17:05 +0100, Erik wrote:

> On 15/06/17 15:10, Chris Angelico wrote:
>> On Fri, Jun 16, 2017 at 12:00 AM, alister 
>> wrote:
>>> Json is designed to be legal Javascript code & therefore directly
>>> executable so no parser is posible.
>>>
>>>
>> "no parser is possible"???
> 
> I *think* alister meant "so it is possible to not use a parser
> [library]" (i.e., parse the stream using JavaScript's parser via eval()
> - though I agree with everyone else who has said this should never be
> done).
> 
> I may be wrong about what alister meant, but the language reminds me of
> a German colleague of mine from a few years back who wrote in some API
> update documentation "Specifying parameter X is no longer an option".
> What he meant was "is now mandatory" or "is no longer optional". To a
> native English speaker, it reads as "can no longer be used" which is the
> opposite of what he meant ...
> 
> E.

indeed, I missed a few words in my statement
caused by me re-composing my thoughts on the fly.
(The non native English speaker excuse is not an option for me) 



-- 
Man invented language to satisfy his deep need to complain.
-- Lily Tomlin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Developers Who Use Spaces Make More Money!

2017-06-16 Thread alister
On Thu, 15 Jun 2017 21:11:41 -0600, Ian Kelly wrote:

> On Thu, Jun 15, 2017 at 8:51 PM, Larry Martell 
> wrote:
>> On Thu, Jun 15, 2017 at 6:35 PM, Christopher Reimer
>>  wrote:
>>> One commentator on a tech website admonished programmers for wasting
>>> time by pressing the space bar four times instead of using tab. O_o
>>
>> Not in vi with
>>
>> set autoindent set expandtab
> 
> Or in any halfway-decent editor with reasonable indentation settings.

indeed if your editor can deal with something this simple i dread to 
think how much more time wasting will be caused by all of its other 
inefficiencies



-- 
Instead, people would take pains to tell her that beauty was only
skin-deep, as if a man ever fell for an attractive pair of kidneys.
(Maskerade)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Standard lib version of something like enumerate() that takes a max count iteration parameter?

2017-06-16 Thread Andre Müller
Am 15.06.2017 um 07:09 schrieb Jussi Piitulainen:
> Andre Müller writes:
>
>> I'm a fan of infinite sequences. Try out itertools.islice.
>> You should not underestimate this very important module.
>>
>> Please read also the documentation:
>> https://docs.python.org/3.6/library/itertools.html
>>
>> from itertools import islice
>>
>> iterable = range(100)
>> # since Python 3 range is a lazy evaluated object
>> # using this just as a dummy
>> # if you're using legacy Python (2.x), then use the xrange function for it
>> # or you'll get a memory error
>>
>> max_count = 10
>> step = 1
>>
>> for i, element in enumerate(islice(iterable, 0, max_count, step), start=1):
>> print(i, element)
> I like to test this kind of thing with iter("abracadabra") and look at
> the remaining elements, just to be sure that they are still there.
>
> from itertools import islice
>
> s = iter("abracadabra")
> for i, element in enumerate(islice(s, 3)):
> print(i, element)
>
> print(''.join(s))
>
> Prints this:
>
> 0 a
> 1 b
> 2 r
> acadabra
>
> One can do a similar check with iter(range(1000)). The range object
> itself does not change when its elements are accessed.

Very interesting. Normally this should not work.
The behavior is unexpected. So you taught me, what can happen.

Thank You :-)

Normally you don't see very often iter(). If you've short sequences
which are str,
you can just use index access. My example is for something, which is
bigger than memory.
Otherwise you've sometimes objects which doesn't support index access
like sets or generators.
Then you can use this nifty trick.

Instead of using:

s = iter('abracadabra') # no direct access to the str object

You should use:

s = 'abracadabra' # direct access to object
iterator = iter(s) # makes an iterator which is accessing s. The str object 
does not change.
# s is still 'abracadabra'

# also you can make more than one iterator of the same object
iterator2 = iter(s)
iterator3 = iter(s)
iterator4 = iter(s)
# they only rely on s, but they are independent iterators
# s won't change

Another trick is:

# incomplete chunks are left out
list(zip(*[iter(s)]*4))
# -> [('a', 'b', 'r', 'a'), ('c', 'a', 'd', 'a')]

# incomplete chunks will have None in the list
list(itertools.zip_longest(*[iter(s)]*4))
# -> [('a', 'b', 'r', 'a'), ('c', 'a', 'd', 'a'), ('b', 'r', 'a', None)]

# to impress your friends you can do
for chunk in itertools.zip_longest(*[iter(s)]*4):
chunked_str = ''.join(c for c in chunk if c) # generator expression
inside join with condition
print(chunked_str)

It took long time for me to understand this.
Someone has written this nifty tick in the python irc channel.

You should create every time a new iterator with iter, when iterating
over something again.
In your example you're iterating twice over the same iterator.

When you're using for example a for loop, it creates internally an
iterator of your object, if it's supported.
It gets element by element and assigns it to one_char.

# creates iterator for s and iterates over it
for one_char in s:
print(one_char)

# creates again a new iterator for s and iterates over it
for one_char in s:
print(one_char)





signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Standard lib version of something like enumerate() that takes a max count iteration parameter?

2017-06-16 Thread Peter Otten
Andre Müller wrote:

> # to impress your friends you can do
> for chunk in itertools.zip_longest(*[iter(s)]*4):
> chunked_str = ''.join(c for c in chunk if c) # generator expression
> inside join with condition
> print(chunked_str)
 
This can be simplified with a fillvalue

>>> s = "abracadabra"
>>> for chunk in itertools.zip_longest(*[iter(s)]*4, fillvalue=""):
... print("".join(chunk))
... 
abra
cada
bra

but for sequences I prefer regular slicing:

>>> N = 4
>>> for start in range(0, len(s), N):
... print(s[start:start+N])
... 
abra
cada
bra


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


EuroPython 2017: Django Girls Workshop

2017-06-16 Thread M.-A. Lemburg
EuroPython 2017 is happy to host and sponsor a Django Girls free
workshop on Sunday 9th, from 9:00 to 18:00 CEST.

The non-profit organization FuzzyBrains will lead you through
HTML/CSS, Python/Django to the design of a new blog in a single day
workshop. No prior programming knowledge is needed to participate! If
you would like to take part, hurry up and apply for a spot on the
Django Girls website.


   * Django Girls EuroPython 2017 *

https://ep2017.europython.eu/en/events/django-girls-workshop/


EuroPython 2017 sponsors the event, providing room, catering and free
conference tickets for the trainers, together with other facilities.

If you’d like to know more about Django Girls, please check their
website or their social network coordinates on Facebook and Twitter.

EuroPython ticket sales are picking up
--

If you want to join the EuroPython fun for the whole week, be sure to
get your tickets as soon as possible, since ticket sales have picked
up quite a bit after we announced the schedule.


Enjoy,
--
EuroPython 2017 Team
http://ep2017.europython.eu/
http://www.europython-society.org/

PS: Please forward or retweet to help us reach all interested parties:
https://twitter.com/europython/status/875699933269176320
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to calculate Value in (%) and than create new column with percentage of used time. (Python)

2017-06-16 Thread Alla Perlova
Dear folks, I need some help with:
 
I am working with some Hospital data, and I need to calculate Operating Room 
Utilization per day

df.groupby(by = ['Actual_Surgery_Day','Actual_Surgery_Room']) 
.agg({'Actual_Surgery_Duratation' : 'sum', 'Procedure_Code' : 'count'})


   Actual_Surgery_Duratation Procedure_Code

 609.0 10

 561.0   7

 704.0   5

Actual_Surgery_Day  Actual_Surgery_Room

20001
20002
20003
..
20016
2016-01-08


So, I need to calculate a simple following equation **Z=960-x-18(y-1)**

960 min  = optimal time (in min) Room Utilization per day

x = Time for all procedures per Day; column - (Actual_Surgery_Duratation)

y = Procedure (amount per day in each Room (- 1), first case);  column - 
(Procedure_Code)

18 min = break time between each procedure 

How do I  calculate Room Utilization in %?

**Example**,first row table:

z=960-x-18(10-1)
z=960 min (wanted time) - 609 min  (actual time) - 162 (breaktime)
z=189 min time not used

What is the percentage of used time? in % (960 min - 189 min)

I appreciate any help.
Alla
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Customise the virtualenv `activate` script

2017-06-16 Thread Tim Chase
On 2017-06-16 15:53, Ben Finney wrote:
> > I must admit my initial preference would be the differently named
> > wrapper. Surely users of the codebase will be invoking stuff via
> > something opaque which sources the requisite things?  
> 
> That “something opaque” is the ‘$VENV/bin/activate’ script; many
> people who join the team will already know that, and I'm trying to
> make use of that existing convention.
> 
> > Actually, on trying to write something simple and flexible, since
> > once made the venv is basicly state WRT the activate script, I'm
> > leaning towards hacking the activate script, probably by keeping
> > a distinct file off the the side and modifying activate to source
> > it.  
> 
> Yeah, I'd much prefer to be told there's a hook to use, so that
> someone who creates a standard Python virtualenv the conventional
> way will not need to then hack that virtualenv.

At least within virtualenvwrapper (I'm not sure whether they come
with virtualenv proper), in my $WORKON_HOME and
$WORKON_HOME/$VIRTUALENV/bin directories, I have a bunch of pre* and
post* templates including preactivate and postactivate hooks in which
I can put various bash scripting.  I've only used them once or twice
and don't have an example readily at hand, but it seems would give you
what you're looking for.

-tkc


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


Re: [OT] is JSON all that great? - was Re: API Help

2017-06-16 Thread Grant Edwards
On 2017-06-16, Ben Finney  wrote:
> alister  writes:
>
>> Json is designed to be legal Javascript code & therefore directly
>> executable so no parser is posible.
>
> JSON is designed to be *a strictly limited subset* of legal JavaScript
> that only defines data structures. The explicit goal is that it is
> statically parseable as non-executable data.

That doesn't mean that it's reasonable/acceptable practice to eval() a
string from an untrusted source because it _might_ be JSON.

-- 
Grant Edwards   grant.b.edwardsYow! I brought my BOWLING
  at   BALL -- and some DRUGS!!
  gmail.com

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


Re: [OT] is JSON all that great? - was Re: API Help

2017-06-16 Thread Ben Finney
Grant Edwards  writes:

> On 2017-06-16, Ben Finney  wrote:
> > JSON is designed to be *a strictly limited subset* of legal
> > JavaScript that only defines data structures. The explicit goal is
> > that it is statically parseable as non-executable data.
>
> That doesn't mean that it's reasonable/acceptable practice to eval() a
> string from an untrusted source because it _might_ be JSON.

Yes. We appear to be in firm agreement.

-- 
 \“It is always a silly thing to give advice, but to give good |
  `\   advice is absolutely fatal.” —Oscar Wilde, _The Portrait of Mr. |
_o__)  W. H._, 1889-07 |
Ben Finney

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


Re: API Help

2017-06-16 Thread Tobiah

> I still think it _could_ be the output of a Python repr() or similar
> (something that is expected to be evaluated as a Python expression).

It may be valid fodder for python eval(), but if it came from repr() It
would have used single quotes, yes?


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


Instagram: 40% Py3 to 99% Py3 in 10 months

2017-06-16 Thread Terry Reedy

https://thenewstack.io/instagram-makes-smooth-move-python-3/
--
Terry Jan Reedy

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


Re: Instagram: 40% Py3 to 99% Py3 in 10 months

2017-06-16 Thread Rauklei P . S . Guimarães
Very Nice.

Em sex, 16 de jun de 2017 às 13:30, Terry Reedy  escreveu:

> https://thenewstack.io/instagram-makes-smooth-move-python-3/
> --
> Terry Jan Reedy
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Instagram: 40% Py3 to 99% Py3 in 10 months

2017-06-16 Thread Paul Barry
The process they followed is discussed in their recent Keynote at PyCon
2017: https://youtu.be/66XoCk79kjM

Well worth the 40 minutes it takes to watch  :-)

Paul.

On 16 June 2017 at 18:43, Rauklei P. S. Guimarães  wrote:

> Very Nice.
>
> Em sex, 16 de jun de 2017 às 13:30, Terry Reedy 
> escreveu:
>
> > https://thenewstack.io/instagram-makes-smooth-move-python-3/
> > --
> > Terry Jan Reedy
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Paul Barry, t: @barrypj  - w:
http://paulbarry.itcarlow.ie - e: [email protected]
Lecturer, Computer Networking: Institute of Technology, Carlow, Ireland.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: sqlite in 2.7 on redhat 6

2017-06-16 Thread Thomas Jollans
On 15/06/17 14:45, Larry Martell wrote:
> I am trying to use sqlite
> 
> $ python2.7
> Python 2.7.10 (default, Feb 22 2016, 12:13:36)
> [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 import _sqlite3
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named _sqlite3
> 
> It's there at: 
> /opt/rh/python27/root/usr/lib64/python2.7/lib-dynload/_sqlite3.so
> but that is not in my path

This looks like it's installed for an SCL
(https://www.softwarecollections.org/en/scls/rhscl/python27/). Are you
running the SCL python, or some other python?

You either have to run the SCL python (My guess without having an EL6
system lying around: scl enable python27) or install sqlite in whatever
python installation you're actually using.

.
> 
> I tried adding /opt/rh/python27/root/usr/lib64/python2.7/lib-dynload/
> to my path and then it fails with:
> 
> ImportError: libpython2.7.so.1.0: cannot open shared object file: No
> such file or directory
> 
> Isn't sqlite part of the standard lib? Shouldn't this just work?
> 

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


python and post gis question

2017-06-16 Thread Xristos Xristoou
I have create a python script where use post gis queries to automate some 
intersection tasks using postgis database.

in my database I have polygons,points and lines.

here my snippet code of my script :

 try:
if str(geomtype) == 'point':
geomtype = 1
elif str(geomtype) == 'linestring':
geomtype = 2
elif str(geomtype) == 'polygon':
geomtype = 3
else:
raise TypeError()
sql = "\nDROP TABLE IF EXISTS {0}.{1};\nCREATE TABLE {0}.{1} AS (SELECT 
{4},\n(st_dump(ST_CollectionExtract(st_intersection(dbgis.{2}.shape,dbgis.{3}.shape)::GEOMETRY(GEOMETRY,2345),{5}))).geom
 AS shape\nFROM  dbgis.{2}, dbgis.{3} WHERE 
st_intersects(dbgis.{2}.shape,dbgis.{3}.shape) AND {5}=3);\nCREATE INDEX 
idx_{2}_{3}_{6} ON {0}.{1} USING GIST (shape);\nALTER TABLE {0}.{1} ADD COLUMN 
id SERIAL;\nALTER TABLE {0}.{1} ADD COLUMN my_area double precision;\nUPDATE 
{0}.{1} SET my_area = ST_AREA(shape::GEOMETRY);\nALTER TABLE {0}.{1} ADD 
PRIMARY KEY (id);\nSELECT pop_g_col('{0}.{1}'::REGCLASS);\nGRANT ALL ON {0}.{1} 
TO GROUP u_cl;\nGRANT ALL ON {0}.{1} TO GROUP my_user;\n-- VACUUM ANALYZE 
{0}.{1};\n".format(schema, table, tablea, tableb, selects, geomtype, id_gen())
return sql
this post gis sql query work nice but I need the new field where I want to add 
my_area to create only for polygons layers.

that code create for all layers (lines,points,polygons) that field my_area if 
layer is point or line then take value 0 I don't like that I don't need it.

how to change this code to create my_area only in polygons ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] is JSON all that great? - was Re: API Help

2017-06-16 Thread Erik

On 16/06/17 12:03, alister wrote:

(The non native English speaker excuse is not an option for me)


Does that mean it's mandatory for you? I'm confused :D :D

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


Re: Customise the virtualenv `activate` script

2017-06-16 Thread Cameron Simpson

On 16Jun2017 06:40, Tim Chase  wrote:

At least within virtualenvwrapper (I'm not sure whether they come
with virtualenv proper), in my $WORKON_HOME and
$WORKON_HOME/$VIRTUALENV/bin directories, I have a bunch of pre* and
post* templates including preactivate and postactivate hooks in which
I can put various bash scripting.  I've only used them once or twice
and don't have an example readily at hand, but it seems would give you
what you're looking for.


And just for added data, I've just inspected the 'activate" script in my 3.6.1 
virtualenv (which looks like it was made using virtualenv-3.5) and it has no 
hooks in the shell code, so Tim's stuff looks like it comes from 
virtualenvwrapper.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to calculate Value in (%) and than create new column with percentage of used time. (Python)

2017-06-16 Thread mcmxl
I assigned the optimal time of 960min to x.
>>> x=960.0

Then I assigned the time used 189min to y.
>>> y=189.0

Then I divide the time used(y) by the optimal time(x) and multiply the answer 
by 100 and assign the answer to z.
>>> z=(y/x)*100

The answer.
>>> z
19.6875

For Python to give you an accurate answer you must include decimal points.
I hope this helps.
-- 
https://mail.python.org/mailman/listinfo/python-list