Storing value with limits in object

2008-06-22 Thread Josip
I'm trying to limit a value stored by object (either int or float):

class Limited(object):
def __init__(self, value, min, max):
self.min, self.max = min, max
self.n = value
def set_n(self,value):
if value < self.min: # boundary check
self.n = self.min
if value > self.max:
self.n = self.max
else:
self.n = value
n = property(lambda self : self._value, set_n)

This works, except I would like the class to behave like built-in types, so
I can use it like this:

a = Limited(7, 0, 10)
b = math.sin(a)

So that object itself returns it's value (which is stored in a.n). Is this
possible?



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


Re: Storing value with limits in object

2008-06-22 Thread Josip
> Not with normal vars, because = is a rebinding operator in Python,
> rather than assignment.
>
> You can do (close to) the above with object properties.
>
> David.

Yes, but it's done with built-in types like int and float. I suspect I could 
subclass from them and implement limits, but I would have to make
seperate class for each type. Can I override conversion methods like int() 
and float() within my class? 


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


Re: Storing value with limits in object

2008-06-22 Thread Josip
> In theory you could hack Python's internal locals or globals
> dictionary so that it did something unusual while looking up your
> object. But in practice this doesn't work, because the returned
> objects (when you call globals() or locals()) attributes are readonly.
> Probably because those internal lookup dicts are implemented in
> optimized C not Python, and the C implementation doesn't let you
> redefine it's internals via the Python interface.
>
> David.

I'll settle for implementing the __call__() method to return the value as I 
have
no intention to mess around with Python's internal mechanisms.

Thanks a lot for your deep insight. 


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


Re: Storing value with limits in object

2008-06-22 Thread Josip
> Why not make it a function?
>
> function assignLimited(value, vmin, vmax):
> value = max(vmin, value)
> value = min(vmax, value)
> return value
>
>
> a = assignLimited(7, 0, 10)
>
>
> Seems like it solves your problem relatively cleanly.
> Note: I also removed min/max variables because they would mask the
> built-in min/max functions.
>
> -Larry

Yes, the simple solution is often the best. Still, I'm going for object
oriented solution because I want the value and it's limits to be kept
together as I'll have many such values with different limits. Storing all
the limits in caller namespace is not really an option.



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


Re: Storing value with limits in object

2008-06-22 Thread Josip
> Why not make it a function?
>
> function assignLimited(value, vmin, vmax):
> value = max(vmin, value)
> value = min(vmax, value)
> return value
>
>
> a = assignLimited(7, 0, 10)
>
>
> Seems like it solves your problem relatively cleanly.
> Note: I also removed min/max variables because they would mask the
> built-in min/max functions.
>
> -Larry

Yes, the simple solution is often the best. Still, I'm going for object
oriented solution because I want the value and it's limits to be kept
together as I'll have many such values with different limits. Storing all
the limits in caller namespace is not really an option.




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


Re: Storing value with limits in object

2008-06-22 Thread Josip
> I bet you didn't even try this, unless your definition of "works"
> includes a "RuntimeError: maximum recursion depth exceeded". Here's a
> a working version:

Actually, the version I'm using is somewhat bigger. I removed docstrings and
recklessly stripped away some methods to make it shorter concise and
incorrect.

> For (most) math.* functions it suffices to define __float__, so the
> above works. For making it behave (almost) like a regular number,
> you'd have to write many more special methods:
> http://docs.python.org/ref/numeric-types.html.
> Here's a possible start:
>
(...)

Yes, this is very close to what I was looking for. It implements all the
functionality except asssigning values. And speed is not an issue for my
application.
Thanks.



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


Re: learning unit testing in python

2008-06-23 Thread Josip
> Hi all.
>
> I'd like learn some basic unit testing with python.
> I red some articles about different testing framework like unittest or
> nose, but I'm a bit confused: what is the best choice? I'm not a
> professional developer (I'm a SEO) but I belive that unit testing is a
> good and pragmatic way to produce working software, so I'd like to
> find something really simple ad straightforward because I don't have
> to manage big programming projects.
>
> Thanks in advance,
>
> Alex

Have you checked out doctest?
http://docs.python.org/lib/module-doctest.html
It's the best way to add few tests to function or class, you just add
them to docstring.
Unit tests are somewhat demanding as they usualy require creating another
file just for storing them, but are ofcourse more powerful as well. For 
example,
unittest module alows you to execute a section of code before or after each 
test
to initialize values and clean up.

Nose module has few extra features, but it can also run test writen for 
unittest,
so it's easy to switch if you find standard library module lacking for your 
purpose. 


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


Re: Storing value with limits in object

2008-06-26 Thread Josip
Thanks alot. I'm going to use this with few modifications to tailor it to my 
needs.
Thumbs up!

> #!/usr/bin/env python
>
> ## VERSION 2
> ##
> ## changelog:
> ## - Uses inheritance from _Limited
> ## - Added _LimitedLong and llong
> ## - limit choose between int, long, and float
>
> class _Limited(object):
>def setlimits(self, lim):
>''' Set the limits and if value is not within limit,
>raise ValueError
>
>The lim argument accepts:
>- An instance of _Limited, from which to copy the limits
>- A two-tuple, which specifies the limits i.e. (min, max)
>
>If lim isn't those or lim[0] > lim[1], raise
> InvalidLimitsError
>
>Accepting _Limited instance is just for convenience
>'''
>
>if isinstance(lim, _Limited):
>self.min, self.max = lim.limits
>else:
>try:
>self.min, self.max = lim
>if self.min > self.max: raise ValueError
>except (ValueError, TypeError):
>raise self.InvalidLimitsError, ('limit = %s' %
> str(lim))
>
>if not (self.min < self < self.max):
>raise ValueError, \
>  ('val = %s, min = %s, max = %s' % \
>  (self, self.min, self.max))
>
>def getlimits(self):
>return (self.min, self.max)
>
>limits = property(getlimits, setlimits)
>
> class _LimitedInt(int, _Limited):
>def __init__(self, value, base = 10):
>int.__init__(value, base)
>
> class _LimitedLong(long, _Limited):
>def __init__(self, value, base = 10):
>long.__init__(value, base)
>
> class _LimitedFloat(float, _Limited):
>def __init__(self, value):
>float.__init__(value)
>
>
> def lint(value, limits, base = None):
>''' Always Creates _LimitedInt instance, allows the use of base
>'''
>if base:
>ret = _LimitedInt(value, base)
>else:
>ret = _LimitedInt(value)
>
>ret.limits = limits
>return ret
>
> def llong(value, limits, base = None):
>''' Always Creates _LimitedLong instance, allows the use of base
>'''
>if base:
>ret = _LimitedLong(value, base)
>else:
>ret = _LimitedLong(value)
>
>ret.limits = limits
>return ret
>
> def lfloat(value, limits):
>''' Always Creates _LimitedFloat instance
>'''
>ret = _LimitedFloat(value)
>
>ret.limits = limits
>return ret
>
> def limit(value, limits):
>''' Automatically choose between _LimitedInt, _LimitedLong,
>or _LimitedFloat. Cannot use _LimitedInt's/Long's base
>'''
>if isinstance(value, (int, long)):
>try:
>ret = _LimitedInt(value)
>except OverflowError:
>ret = _LimitedLong(value)
>elif isinstance(value, float):
>ret = _LimitedFloat(value)
>
>ret.limits = limits
>return ret 


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


Re: Working with graphs - Kevin Bacon game

2019-01-09 Thread Josip Skako
Thank You for your answer, I fixed everything as You said.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with graphs - Kevin Bacon game

2019-01-09 Thread Josip Skako
Thank You for Your answer,

I am not sure what to try anymore, I guess I have to return "ime" from 
__slots__ at cvor() class to show proper strings and I am not able to do it.

Now I am not sure that I am going at right direction to do Kevin Bacon game and 
will I be able to load this data into graph and find shortest way?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with graphs - Kevin Bacon game

2019-01-09 Thread Josip Skako
I get it now, basically you are accessing class atributes with 
"self.something", thank You.

So now I get this:

"['Apollo 13 (1995)', 'Bill Paxton', 'Tom Hanks', 'Kevin Bacon\n', 'Begyndte 
ombord, Det (1937)', 'Aage Schmidt', 'Valso Holm\n', 'Bersaglio mobile (1967)', 
'Dana Young', 'Bebe Drake\n', 'Bezottsovshchina (1976)', 'Yelena Maksimova', 
'Lev Prygunov\n', 'Dark, The (1979)', 'Angelo Rossitto', 'William Devane\n', 
'Death to Smoochy (2002)',..."

That is great.
What would be the best way now to find shortest path between ex. Tom Hanks and 
ex. William Devane?

I should get something like: https://oracleofbacon.org/

Should I insert this data into networkx somehow, would it be easier?
-- 
https://mail.python.org/mailman/listinfo/python-list