Re: Flubbed it in the second interation through the string: range error... HOW?

2024-05-28 Thread Cameron Simpson via Python-list

On 29May2024 01:14, Thomas Passin  wrote:
Also, it's 2024 ... time to start using f-strings (because they are 
more readable than str.format())


By which Thomas means stuff like this:

print(f'if block {name[index]} and index {index}')

Notice the leading "f'". Personally I wouldn't even go that far, just:

print('if block', name[index], 'and index', index)

But there are plenty of places where f-strings are very useful.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Flubbed it in the second interation through the string: range error... HOW?

2024-05-28 Thread Thomas Passin via Python-list
Your code is unreadable. The lines have all run together.  And after 
that, kindly explain what you want your code sample to do.  "Process" 
doesn't say much.


From what I can make out about what you are trying to do, you would do 
better to index through your string with


for i, chr in enumerate(name):
# do something with the character

Also, it's 2024 ... time to start using f-strings (because they are more 
readable than str.format())


On 5/29/2024 12:33 AM, Kevin M. Wilson via Python-list wrote:

The following is my effort to understand how to process a string, letter, by 
letter:
def myfunc(name):        index = 0    howmax = len(name)    # while (index <= 
howmax):    while (index < howmax):        if (index % 2 == 0):            
print('letter to upper = {}, index {}!'.format(name[index], index))            name = 
name[index].upper()            print('if block {} and index {}'.format(name[index], 
index))        elif (index % 2 > 0):            print(index)            print('Start: 
elseif block, index is {}, letter is {}'.format(index, name))            # print('letter 
to lower = {}'.format(name[index]))            # print('Already lowercase do noting: 
name = {}'.format(name[index]))        index += 1        # index = name.upper()
     return name
myfunc('capitalism')
Error message:                        Not making sense, index is 1, letter s/b 
'a'letter to upper = c, index 0!
if block C and index 0
1
Start: elseif block, index is 1, letter is C
---
IndexErrorTraceback (most recent call last)
Cell In[27], line 21
  17 # index = name.upper()
  19 return name
---> 21 myfunc('capitalism')

Cell In[27], line 8, in myfunc(name)
   6 while (index < howmax):
   7 if (index % 2 == 0):
> 8 print('letter to upper = {}, index {}!'.format(name[index], 
index))
   9 name = name[index].upper()
  10 print('if block {} and index {}'.format(name[index], index))

IndexError: string index out of 
range***
So, I'm doing something... Stupid!!
***
"When you pass through the waters, I will be with you: and when you pass through the 
rivers, they will not sweep over you. When you walk through the fire, you will not be 
burned: the flames will not set you ablaze."
Isaiah 43:2


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


Fw: Flubbed it in the second interation through the string: range error... HOW?

2024-05-28 Thread Kevin M. Wilson via Python-list
The format in this email is not of my making, should someone know, how to do 
this so that it's a readable script do tell!
KMW

***
"When you pass through the waters, I will be with you: and when you pass 
through the rivers, they will not sweep over you. When you walk through the 
fire, you will not be burned: the flames will not set you ablaze."      
Isaiah 43:2
 

   - Forwarded Message - From: Kevin M. Wilson via Python-list 
To: python-list@python.org 
Sent: Tuesday, May 28, 2024 at 10:35:23 PM MDTSubject: 
Flubbed it in the second interation through the string: range error... HOW?
 The following is my effort to understand how to process a string, letter, by 
letter:
def myfunc(name):        index = 0    howmax = len(name)    # while (index <= 
howmax):    while (index < howmax):        if (index % 2 == 0):            
print('letter to upper = {}, index {}!'.format(name[index], index))            
name = name[index].upper()            print('if block {} and index 
{}'.format(name[index], index))        elif (index % 2 > 0):            
print(index)            print('Start: elseif block, index is {}, letter is 
{}'.format(index, name))            # print('letter to lower = 
{}'.format(name[index]))            # print('Already lowercase do noting: name 
= {}'.format(name[index]))        index += 1        # index = name.upper()      
  
    return name        
myfunc('capitalism')
Error message:                        Not making sense, index is 1, letter s/b 
'a'letter to upper = c, index 0!
if block C and index 0
1
Start: elseif block, index is 1, letter is C
---
IndexError                                Traceback (most recent call last)
Cell In[27], line 21
    17        # index = name.upper()        
    19    return name        
---> 21 myfunc('capitalism')

Cell In[27], line 8, in myfunc(name)
      6 while (index < howmax):
      7    if (index % 2 == 0):
> 8        print('letter to upper = {}, index {}!'.format(name[index], 
index))
      9        name = name[index].upper()
    10        print('if block {} and index {}'.format(name[index], index))

IndexError: string index out of 
range***
So, I'm doing something... Stupid!!
***
"When you pass through the waters, I will be with you: and when you pass 
through the rivers, they will not sweep over you. When you walk through the 
fire, you will not be burned: the flames will not set you ablaze."      
Isaiah 43:2
-- 
https://mail.python.org/mailman/listinfo/python-list
  
-- 
https://mail.python.org/mailman/listinfo/python-list


Flubbed it in the second interation through the string: range error... HOW?

2024-05-28 Thread Kevin M. Wilson via Python-list
The following is my effort to understand how to process a string, letter, by 
letter:
def myfunc(name):        index = 0    howmax = len(name)    # while (index <= 
howmax):    while (index < howmax):        if (index % 2 == 0):            
print('letter to upper = {}, index {}!'.format(name[index], index))            
name = name[index].upper()            print('if block {} and index 
{}'.format(name[index], index))        elif (index % 2 > 0):            
print(index)            print('Start: elseif block, index is {}, letter is 
{}'.format(index, name))            # print('letter to lower = 
{}'.format(name[index]))            # print('Already lowercase do noting: name 
= {}'.format(name[index]))        index += 1        # index = name.upper()      
  
    return name        
myfunc('capitalism')
Error message:                        Not making sense, index is 1, letter s/b 
'a'letter to upper = c, index 0!
if block C and index 0
1
Start: elseif block, index is 1, letter is C
---
IndexErrorTraceback (most recent call last)
Cell In[27], line 21
 17 # index = name.upper()
 19 return name
---> 21 myfunc('capitalism')

Cell In[27], line 8, in myfunc(name)
  6 while (index < howmax):
  7 if (index % 2 == 0):
> 8 print('letter to upper = {}, index {}!'.format(name[index], 
index))
  9 name = name[index].upper()
 10 print('if block {} and index {}'.format(name[index], index))

IndexError: string index out of 
range***
So, I'm doing something... Stupid!!
***
"When you pass through the waters, I will be with you: and when you pass 
through the rivers, they will not sweep over you. When you walk through the 
fire, you will not be burned: the flames will not set you ablaze."      
Isaiah 43:2
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Serializing pydantic enums

2024-05-28 Thread Left Right via Python-list
Most Python objects aren't serializable into JSON. Pydantic isn't
special in this sense.

What can you do about this? -- Well, if this is a one-of situation,
then, maybe just do it by hand?

If this is a recurring problem: json.dumps() takes a cls argument that
will be used to do the serialization. Extend json.JSONEncoder and
implement the encode() method for the encoder class you are passing. I
believe that the official docs have some information about this too.

On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
 wrote:
>
> Just getting started with pydantic. I have this example code:
>
> class FinishReason(Enum):
> stop = 'stop'
>
> class Choice(BaseModel):
> finish_reason: FinishReason = Field(...)
>
>
> But I cannot serialize this:
>
> json.dumps(Choice(finish_reason=FinishReason.stop).dict())
> *** TypeError: Object of type FinishReason is not JSON serializable
>
>
> I get the object not the value:
>
> (Pdb) Choice(finish_reason=FinishReason.stop)
> Choice(finish_reason=)
>
>
> Also tried it with .value, same result.
>
> What am I missing here?
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Serializing pydantic enums

2024-05-28 Thread Larry Martell via Python-list
Just getting started with pydantic. I have this example code:

class FinishReason(Enum):
stop = 'stop'

class Choice(BaseModel):
finish_reason: FinishReason = Field(...)


But I cannot serialize this:

json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable


I get the object not the value:

(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=)


Also tried it with .value, same result.

What am I missing here?
-- 
https://mail.python.org/mailman/listinfo/python-list