Skip to main content

An example Python IOC looks like Spring

Project description

PyBean

1. What is this?

This is a Python module implementation of IOC container management.

You can read your XML files and dynamically manage your Python classes through the "Application Context" class. In order to help those familiar with Java Spring understand quickly, I plan to try to adapt to the rules in Spring as much as possible in the future.

2.How to use?

For Developers, ensure that Python is installed on your device and that the environment is ready.

Download in shell by pip:

pip install pyBean

or

pip3 install pyBean

File structure

project
  +-- dao
  |    +-- __init__.py
  |    +-- person.py
  |  
  +-- resource
  |     +-- applicationContext.xml
  +-- main.py

Prepare the classes you want to inject

# person.py
class PersonDao:
    def __init__(self, name):
        self.name = name
        self.home = ""

    def greet(self):
        sentence = f"Hey {self.name}!"
        if self.home:
            sentence += f" {self.home} is a good place!"
        print(sentence)

Prepare the XML file, named applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans resource="https://pypi.org/project/pyBean/">
    <bean id="me" class="dao.person.PersonDao">
        <!--person name-->
        <constructor-arg value="Lee"/>
        
        <!--you can change it to your region here.-->
        <property name="home" value="China"/>
        
    </bean>
    
</beans>

In your python file:

# main.py

from PyBean.ioc import ApplicationContext, ApplicationMode


if __name__ == '__main__':
    actx = ApplicationContext('resource/applicationContext.xml')
    personGet = actx.getBean("me")
    personGet.greet()
    

Run it.

python main.py

or

python3 main.py

Hey Lee! China is a good place!

You can turn on development mode to ensure that each getBean operation will re-read the latest content in the XML.

actx = ApplicationContext('resource/applicationContext.xml', 
applicationMode=ApplicationMode.development)

or

actx.set__mode(ApplicationMode.development)

You can add a second type parameter to getBean to constrain the obtained object to belong to that type. Of course, make sure that the type name in your XML is an absolute class path.

from dao.person import PersonDao
personGet = actx.getBean("me", requiredType=PersonDao)

website: https://space.bilibili.com/178065252

pypi: https://pypi.org/project/pyBean/

repository: https://github.com/Liruochen1207/PyBean

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyBean-0.0.26.tar.gz (8.6 kB view details)

Uploaded Source

File details

Details for the file pyBean-0.0.26.tar.gz.

File metadata

  • Download URL: pyBean-0.0.26.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.2

File hashes

Hashes for pyBean-0.0.26.tar.gz
Algorithm Hash digest
SHA256 a050b1c18c211886a68572e132bd1774dcff8500e4a0c416fd5c6b5d15695905
MD5 f256393edf3be14f45ecc125ff052623
BLAKE2b-256 3e7967e2613f040384590a570cc29c7ce72ffba8ab3b0df4bea3dc0942073e3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page