Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread dieter
Antoon Pardon  writes:
> What I am trying to do is the following.
>
> class MyClass (...) :
> @register
> def MyFunction(...)
> ...
>
> What I would want is for the register decorator to somehow create/mutate
> class variable(s) of MyClass.
>
> Is that possible or do I have to rethink my approach?

As others have already explained: the decoration works an the
function (not the method) level. A function knows nothing of a class.

Others have already pointed out work arounds.
I add an additional one:
Instead of:
  class C:
...
@decorate
def f(...): ...
...
you can use:
  class C:
...
def f(...): ...
...
  decorate(C, C.f)

In Python 2, "C.f" returns a method (an object with a
reference to the class and the function) - there, you would
not need the class parameter for "decorate".
In Python 3, however, "C.f" is the function (without any reference
to the class.

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


Re: pandas loc on str lower for column comparison

2019-09-04 Thread Sayth Renshaw
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 NameFTE Agent IDCurrent Leader  New Leader  
Current TeamNew TeamCurrent SiteNew SiteUnnamed: 10 
Unnamed: 11 Unnamed: 12 

How do I misunderstand iloc?

Thanks,
Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Formatting floating point

2019-09-04 Thread DL Neil via Python-list

On 5/09/19 5:12 AM, Dave via Python-list wrote:
...

My question is why, and where do I find a reliable source of information 
on formatting numbers?  Not interested in replacement values like '{} 
{}'.format(1, 2).



Agreed: there's ton(ne)s of information 'out there', much of it old, eg 
Python2, "formatter" (deprecated since v3.4), methods pre-dating 
f-strings, etc; and more of it rather casually thrown-out-there. 
Learning from StackOverflow (etc) has its limits/perils!


Authoritative Python docs: https://docs.python.org/3/
The "Mini-Language": 
https://docs.python.org/3/library/string.html#formatspec


Current advice (v3.6+) is to study the newer f-strings (formally 
"formatted string literals"): 
https://docs.python.org/3/tutorial/inputoutput.html which came from PEP 
498: https://www.python.org/dev/peps/pep-0498/


--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to remove a string from a txt file?

2019-09-04 Thread Tobiah

On 9/4/19 8:08 AM, Spencer Du wrote:

Hi

I want to remove a string from a txt file and then print out what I have 
removed. How do I do this.

The txt file is in this format and should be kept in this format.

txt.txt:
laser,cameras,

Thanks



Do you want to remove one of the fields by using an argument?  Like:

my_py_solution txt.txt cameras

Its not clear what you are trying to achieve.



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


Re: How to remove a string from a txt file?

2019-09-04 Thread DL Neil via Python-list

On 5/09/19 3:08 AM, Spencer Du wrote:

Hi

I want to remove a string from a txt file and then print out what I have 
removed. How do I do this.

The txt file is in this format and should be kept in this format.

txt.txt:
laser,cameras,


Is this a homework assignment?

What code do you have so far?

How to identify the beginning of the sub-string to be removed?

How to identify the end of the sub-string?

How does one "remove a string" AND "kept in this format"?

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to remove a string from a txt file?

2019-09-04 Thread Joel Goldstick
On Wed, Sep 4, 2019 at 11:11 AM Spencer Du  wrote:
>
> Hi
>
> I want to remove a string from a txt file and then print out what I have 
> removed. How do I do this.
>
> The txt file is in this format and should be kept in this format.
>
> txt.txt:
> laser,cameras,
>
> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list

read the file line by line.  if it doesn't contain your string, write
it to a new file.  for each line, use string functions to find the
string you want to remove.  One way is to split on ',' or use
csvreader to get a list of words between comma.  Print the one you
like and join the values on each side of the text you are looking for.
Write that to a file

Lather, rinse and repeat

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


CVE-2019-9636 - Can this be exploit over the wire?

2019-09-04 Thread Barry Scott
I have been looking into CVE-2019-9636 and I'm not sure that
python code that works in bytes is vulnerable to this.

The "trick" that to make the CVE dangerous assumes that you
have a unicode string with \uff03 (FULLWIDTH NUMBER SIGN')
that under NFKC turns into '#'.

The discussion in https://bugs.python.org/issue36216 all the explaination
starts with  unicode string.

What I'm interested in what happens if you get the URL as part of a HTML page or
other mechanism.

To that end I made a URL that if the vulnerability is triggered will change 
which search
engine is visited.

b'http://google.xn--combing-xr93b.com/fred'

And when I use urlsplit() I get this:

print( urlparse.urlsplit('http://google.xn--combing-xr93b.com/fred') )
SplitResult(scheme='http', netloc='google.xn--combing-xr93b.com', 
path='/fred', query='', fragment='')


The netloc is still IDNA encoded so the "trick" did not trigger.
If code then uses that netloc its going to fail to return anything as no
domain name registrar should have register a name with illegal \uff03 in it.

Also this raises an exception:

'google.xn--combing-xr93b.com'.decode('idna')

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib64/python2.7/encodings/idna.py", line 193, in decode
result.append(ToUnicode(label))
  File "/usr/lib64/python2.7/encodings/idna.py", line 139, in ToUnicode
raise UnicodeError("IDNA does not round-trip", label, label2)
UnicodeError: ('IDNA does not round-trip', 'xn--combing-xr93b', 
'com#bing')

The conclusion I reached is that the CVE only applies to client code that 
allows a URL
in unicode to be entered.

Have I missed something important in the analysis?

Barry

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


Re: Formatting floating point

2019-09-04 Thread Dave via Python-list

On 9/4/19 1:38 PM, Rhodri James wrote:

On 04/09/2019 18:12, Dave via Python-list wrote:
My question is why, and where do I find a reliable source of 
information on formatting numbers?  Not interested in replacement 
values like '{} {}'.format(1, 2).


Here: 
https://docs.python.org/3/library/string.html#format-specification-mini-language 



I suspect the thing you were overlooking was that format() expects to be 
given a format specification, and you gave it a format string.  Instead 
of format(num, "{0:.1f}"), you wanted format(num, ".1f").  If you had 
tried "{0:.1f}".format(num) instead, you would have found that worked too.



Thanks Rhodri

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


Re: Formatting floating point

2019-09-04 Thread Dave via Python-list

On 9/4/19 1:25 PM, Chris Angelico wrote:

On Thu, Sep 5, 2019 at 3:16 AM Dave via Python-list
 wrote:


All,

I have been going in circles trying to format a floating point number so
there is only 1 decimal place.  In reading all of the gobble-gook that
passes for Python advice, it looked like I should have done this:

numStr = '3.14159'
num = float(numstr) # OK so far
numFmt = format(num, '{0:.1f}') # Errors on format string
# --- Alternative that also does not work
numFmt = float("{0:.1f}".format(numStr))
# ---
numFmt = format(num, '0.1f')# Does work



Let's start by eliminating a few possibilities here. Don't try
formatting the original string, and don't call float() on the result
afterwards; just start with a floating-point value, and then create a
formatted string. If you think about starting with a value (which in
this case is a number) and the need to make a displayable version
(which implies that it's a string), your options basically look like
this:

num = 3.14159
num_fmt = format(num, ".1f")
num_fmt = "{0:.1f}".format(num)
num_fmt = f"{num:.1f}"

All of these will give you back the string "3.1". All of them involve
a format string of ".1f", and they differ only in how they're choosing
which value to put there.

Hopefully that will clear things up a bit.

ChrisA



That helps!  Thanks Chris.

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


Re: Formatting floating point

2019-09-04 Thread Rhodri James

On 04/09/2019 18:12, Dave via Python-list wrote:
My question is why, and where do I find a reliable source of information 
on formatting numbers?  Not interested in replacement values like '{} 
{}'.format(1, 2).


Here: 
https://docs.python.org/3/library/string.html#format-specification-mini-language


I suspect the thing you were overlooking was that format() expects to be 
given a format specification, and you gave it a format string.  Instead 
of format(num, "{0:.1f}"), you wanted format(num, ".1f").  If you had 
tried "{0:.1f}".format(num) instead, you would have found that worked too.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Formatting floating point

2019-09-04 Thread Chris Angelico
On Thu, Sep 5, 2019 at 3:16 AM Dave via Python-list
 wrote:
>
> All,
>
> I have been going in circles trying to format a floating point number so
> there is only 1 decimal place.  In reading all of the gobble-gook that
> passes for Python advice, it looked like I should have done this:
>
> numStr = '3.14159'
> num = float(numstr) # OK so far
> numFmt = format(num, '{0:.1f}') # Errors on format string
> # --- Alternative that also does not work
> numFmt = float("{0:.1f}".format(numStr))
> # ---
> numFmt = format(num, '0.1f')# Does work
>

Let's start by eliminating a few possibilities here. Don't try
formatting the original string, and don't call float() on the result
afterwards; just start with a floating-point value, and then create a
formatted string. If you think about starting with a value (which in
this case is a number) and the need to make a displayable version
(which implies that it's a string), your options basically look like
this:

num = 3.14159
num_fmt = format(num, ".1f")
num_fmt = "{0:.1f}".format(num)
num_fmt = f"{num:.1f}"

All of these will give you back the string "3.1". All of them involve
a format string of ".1f", and they differ only in how they're choosing
which value to put there.

Hopefully that will clear things up a bit.

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


Formatting floating point

2019-09-04 Thread Dave via Python-list

All,

I have been going in circles trying to format a floating point number so 
there is only 1 decimal place.  In reading all of the gobble-gook that 
passes for Python advice, it looked like I should have done this:


numStr = '3.14159'
num = float(numstr) # OK so far
numFmt = format(num, '{0:.1f}') # Errors on format string
# --- Alternative that also does not work
numFmt = float("{0:.1f}".format(numStr))
# ---
numFmt = format(num, '0.1f')# Does work

My question is why, and where do I find a reliable source of information 
on formatting numbers?  Not interested in replacement values like '{} 
{}'.format(1, 2).


Thanks,
Dave

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


Re: Proper way to pass Queue to process when using multiprocessing.imap()?

2019-09-04 Thread Chris Angelico
On Thu, Sep 5, 2019 at 2:07 AM Israel Brewster  wrote:
>
> >
> > On Sep 3, 2019, at 11:09 AM, Israel Brewster  wrote:
> >
> >>
> >> On Sep 3, 2019, at 10:49 AM, Peter Otten <__pete...@web.de> wrote:
> >>
> >> Israel Brewster wrote:
> >>
> >>> When using pool.imap to apply a function over a list of values, what is
> >>> the proper way to pass additional arguments to the function, specifically
> >>> in my case a Queue that the process can use to communicate back to the
> >>> main thread (for the purpose of reporting progress)? I have seen
> >>> suggestions of using starmap, but this doesn’t appear to have a “lazy”
> >>> variant, which I have found to be very beneficial in my use case. The
> >>> Queue is the same one for all processes, if that makes a difference.
> >>>
> >>> I could just make the Queue global, but I have always been told not too.
> >>> Perhaps this is an exception?
> >>
> >> How about wrapping the function into another function that takes only one
> >> argument? A concise way is to do that with functools.partial():
> >>
> >> def f(value, queue): ...
> >>
> >> pool.imap(partial(f, queue=...), values)
> >
> > That looks like exactly what I was looking for. I’ll give it a shot. Thanks!
>
> So as it turns out, this doesn’t work after all. I get an error stating that 
> “Queue objects should only be shared between processes through inheritance”. 
> Still a good technique to know though!
>

Globals aren't as bad as some people think. In this case, a
module-level variable seems like the correct way to do things.

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


Re: Proper way to pass Queue to process when using multiprocessing.imap()?

2019-09-04 Thread Israel Brewster
> 
> On Sep 3, 2019, at 11:09 AM, Israel Brewster  wrote:
> 
>> 
>> On Sep 3, 2019, at 10:49 AM, Peter Otten <__pete...@web.de> wrote:
>> 
>> Israel Brewster wrote:
>> 
>>> When using pool.imap to apply a function over a list of values, what is
>>> the proper way to pass additional arguments to the function, specifically
>>> in my case a Queue that the process can use to communicate back to the
>>> main thread (for the purpose of reporting progress)? I have seen
>>> suggestions of using starmap, but this doesn’t appear to have a “lazy”
>>> variant, which I have found to be very beneficial in my use case. The
>>> Queue is the same one for all processes, if that makes a difference.
>>> 
>>> I could just make the Queue global, but I have always been told not too.
>>> Perhaps this is an exception?
>> 
>> How about wrapping the function into another function that takes only one 
>> argument? A concise way is to do that with functools.partial():
>> 
>> def f(value, queue): ...
>> 
>> pool.imap(partial(f, queue=...), values)
> 
> That looks like exactly what I was looking for. I’ll give it a shot. Thanks!

So as it turns out, this doesn’t work after all. I get an error stating that 
“Queue objects should only be shared between processes through inheritance”. 
Still a good technique to know though!

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

> 
> ---
> Israel Brewster
> Software Engineer
> Alaska Volcano Observatory 
> Geophysical Institute - UAF 
> 2156 Koyukuk Drive 
> Fairbanks AK 99775-7320
> Work: 907-474-5172
> cell:  907-328-9145
> 
>> 
>> 
>> 
>>> 
>>> ---
>>> Israel Brewster
>>> Software Engineer
>>> Alaska Volcano Observatory
>>> Geophysical Institute - UAF
>>> 2156 Koyukuk Drive
>>> Fairbanks AK 99775-7320
>>> Work: 907-474-5172
>>> cell:  907-328-9145
>>> 
>> 
>> 
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list 
>> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to only read words within brackets/ parentheses (in .txt file) using Python

2019-09-04 Thread Peter Otten
A S wrote:

> I understand that reading lines in .txt files would look something like
> this in Python:
> 
> 
> with open('filename','r') as fd:
>lines = fd.readlines()
> 
> 
> However, how do I run my code to only read the words in my .txt files that
> are within each balanced parenthesis?
> 
> I am not sure how to go about it, let's say my .txt file contents lines
> like this:
> 
> k;
> 
> select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
> quit;



> The main idea is to read only these portions of the .txt file (i.e. Those
> within parentheses):
> 
>  ("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
> quit;

But jdfjh... is not within parens...
and what about quoted parens "("? Do they count?

You probably need a tokenizer for the SQL dialect used in your "text" file.
But first: can you give a non-technical description of what problem you are 
trying to solve instead of how you want to solve it?

Perhaps someone here knows of a better approach than counting parens.

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


Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Peter Otten
Antoon Pardon wrote:

> What I am trying to do is the following.
> 
> class MyClass (...) :
> @register
> def MyFunction(...)
> ...
> 
> What I would want is for the register decorator to somehow create/mutate
> class variable(s) of MyClass.
> 
> Is that possible or do I have to rethink my approach?

If you are willing to delegate the actual work to the metaclass call: 

def register(f):
f.registered = True
return f

def registered(name, bases, namespace):
namespace["my_cool_functions"] = [
n for n, v in namespace.items()
if getattr(v, "registered", False)
]
return type(name, bases, namespace)

class MyClass(metaclass=registered) :
@register
def foo(self):
pass
@register
def bar(self):
pass
def other(self):
pass

print(MyClass.my_cool_functions)


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


How to only read words within brackets/ parentheses (in .txt file) using Python

2019-09-04 Thread A S
I understand that reading lines in .txt files would look something like this in 
Python:


with open('filename','r') as fd:
   lines = fd.readlines()


However, how do I run my code to only read the words in my .txt files that are 
within each balanced parenthesis?

I am not sure how to go about it, let's say my .txt file contents lines like 
this:

k;

select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
quit; 

/* 1.x FROM _x_Ex_x */ 
proc sql; "TRUuuuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
   where (xxx(xx_ix as format '-xx') gff &jfjfsj_jfjfj.) and 
  (xxx(xx_ix as format '-xx') lec &jgjsd_vnv.)
);


The main idea is to read only these portions of the .txt file (i.e. Those 
within parentheses):

 ("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
quit; 

/* 1.x FROM _x_Ex_x */ 
proc sql; "TRUuuuth")

(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
   where (xxx(xx_ix as format '-xx') gff &jfjfsj_jfjfj.) and 
  (xxx(xx_ix as format '-xx') lec &jgjsd_vnv.)
)



Any help will be truly appreciated
-- 
https://mail.python.org/mailman/listinfo/python-list


How to remove a string from a txt file?

2019-09-04 Thread Spencer Du
Hi

I want to remove a string from a txt file and then print out what I have 
removed. How do I do this.

The txt file is in this format and should be kept in this format.

txt.txt:
laser,cameras,

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


Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Chris Angelico
On Thu, Sep 5, 2019 at 12:23 AM Antoon Pardon  wrote:
>
> What I am trying to do is the following.
>
> class MyClass (...) :
> @register
> def MyFunction(...)
> ...
>
> What I would want is for the register decorator to somehow create/mutate
> class variable(s) of MyClass.
>
> Is that possible or do I have to rethink my approach?
>

At the time when the decorator runs, the class doesn't actually exist.
But if you're wrapping MyFunction (which appears to be a method), then
your wrapper function will receive  'self' as its first parameter, and
can access the class from that. Doesn't help at decoration time,
though.

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


Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Rhodri James

On 04/09/2019 15:21, Antoon Pardon wrote:

What I am trying to do is the following.

class MyClass (...) :
 @register
 def MyFunction(...)
 ...

What I would want is for the register decorator to somehow create/mutate
class variable(s) of MyClass.

Is that possible or do I have to rethink my approach?


I can't see a way of doing that directly, but you could cheat by putting 
the "class variables" in a global dictionary?  And perhaps referencing 
that global from a class variable?  Something like:


my_class_registry = {}

class MyClass(...):
MyClassRegistry = my_class_registry

@register(my_class_registry)
def MyFunction(...):
...

Or you may be able to achieve what you want with dir() and some careful 
naming?


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Antoon Pardon
What I am trying to do is the following.

class MyClass (...) :
@register
def MyFunction(...)
...

What I would want is for the register decorator to somehow create/mutate
class variable(s) of MyClass.

Is that possible or do I have to rethink my approach?

-- 
Antoon Pardon.

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


failed to install anaconda packages

2019-09-04 Thread Mangwendeza Simbarashe


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