Python Forum
Pass results of expression to another expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass results of expression to another expression
#1
I am attempting to pass the results of an expression to another expression. Here is an example of the code I am working with. For reference, this is for connecting to Packet.com to automate spinning up VMs. If I am wrong with my terminology, please forgive me:

import packet
from packet.IPAddress import IPAddress

manager = packet.Manager(auth_token="myapitoken")
params = {
    'per_page': 50
}
device_list = manager.list_devices(project_id='myprojectid', params=params)

print(device_list)
The print function will give me an output containing the VM's UUID:

Output:
[Device: UUID]
What I would like to do instead is take that info, remove 'Device: ' and pass it to another expression so that I can pull data about the VM. Currently, I have to manually enter the devices UUID in as a string:

device = manager.get_device('UUID')
ip = device.ip_addresses
print(ip)
How do I pass the results of device_list formatted without [Device: '] to manager.get_device()?
Reply
#2
If it's always going to look exactly like that, you can just slice the string
>>> "[Device: UUID]"[9:-1]
'UUID'
If the string might vary more, you'd have to get more creative.
Reply
#3
Hold on, is devices_list actually a list of strings though? I'd really be surprised if it was - it's more likely a list of objects. Why do I think that? It's because printing things is usually not the most interesting part of a program, so passing strings around is usually not what you want to be doing. You want to pass objects around, so you can operate on them in a meaningful, natural way. You can already see that the packet library you're using has classes like Manager and IPAddress, so I suspect there's one called Device as well and those objects are what list_devices returns.

What you're seeing when you print that list is the string representation of the objects in there. An example may help:

class Foo(object):
    def __init__(self, x):
        self.x = x

    def __str__(self):
        return "Foo: {}".format(self.x)

    def __repr__(self):
        return self.__str__()

class Bar(object):
    def __init__(self, x):
        self.x = x


foos = [Foo(1), Foo(2), Foo(3)]
print(foos)
bars = [Bar(1), Bar(2), Bar(3)]
print(bars)
with output:

Output:
$ python3 str_repr.py [Foo: 1, Foo: 2, Foo: 3] [<__main__.Bar object at 0x7f289ccdd898>, <__main__.Bar object at 0x7f289ccdd8d0>, <__main__.Bar object at 0x7f289ccdd908>]
You can see that the list of Foos is printed out with a nice readable string for each instance and that's because I've provided implementations for the __str__ and __repr__ methods (I'm not sure which I needed, so I did both!). For Bar though, I didn't implement those methods, so you get the default behaviour.

So, check whether you're actually getting class instances back and if so, find out how to get the information you need from them (i.e. which methods to use and/or fields to read). Both of these questions should be answered by the documentation for the library.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 338 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 758 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  yield usage as statement or expression akbarza 5 803 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  difference between statement and expression akbarza 6 856 Oct-18-2023, 07:40 AM
Last Post: akbarza
  Python beginner that needs an expression added to existing script markham 1 703 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  How to pass encrypted pass to pyodbc script tester_V 0 857 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Regex Include and Exclude patterns in Same Expression starzar 2 791 May-23-2023, 09:12 AM
Last Post: Gribouillis
  Is the following code returning a generator expression? quazirfan 8 1,626 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  Unknown Expression Led_Zeppelin 5 1,930 Oct-15-2022, 12:14 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 1,668 Sep-08-2022, 06:57 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020