olivia_finder.package
Define the data structure of a package
from olivia_finder.package import Package
package = Package(
"AER", "3.1.1", "https://cran.r-project.org/web/packages/AER/index.html",
[
Package("car", "3.0-10", "https://cran.r-project.org/web/packages/car/index.html", []),
Package("effects", "4.2-0","https://cran.r-project.org/web/packages/effects/index.html", []),
Package("foreign", "0.8-80", "https://cran.r-project.org/web/packages/foreign/index.html", []),
]
)
package.print()
Package:
name: AER
version: 3.1.1
url: https://cran.r-project.org/web/packages/AER/index.html
dependencies:
car:3.0-10
effects:4.2-0
foreign:0.8-80
package.to_dict()
{'name': 'AER',
'version': '3.1.1',
'url': 'https://cran.r-project.org/web/packages/AER/index.html',
'dependencies': [{'name': 'car',
'version': '3.0-10',
'url': 'https://cran.r-project.org/web/packages/car/index.html',
'dependencies': []},
{'name': 'effects',
'version': '4.2-0',
'url': 'https://cran.r-project.org/web/packages/effects/index.html',
'dependencies': []},
{'name': 'foreign',
'version': '0.8-80',
'url': 'https://cran.r-project.org/web/packages/foreign/index.html',
'dependencies': []}]}
package.load(
{
"name": "AER",
"version": "3.1.1",
"url": "https://cran.r-project.org/web/packages/AER/index.html",
"dependencies": [
{
"name": "car",
"version": "3.0-10",
"url": "https://cran.r-project.org/web/packages/car/index.html",
"dependencies": []
},
{
"name": "effects",
"version": "4.2-0",
"url": "https://cran.r-project.org/web/packages/effects/index.html",
"dependencies": []
},
{
"name": "foreign",
"version": "0.8-80",
"url": "https://cran.r-project.org/web/packages/foreign/index.html",
"dependencies": []
}
]
}
)
<olivia_finder.package.Package at 0x7f3c2eb5c2e0>
1""" 2 3Define the data structure of a package 4 5 6 7```python 8from olivia_finder.package import Package 9 10 11package = Package( 12 "AER", "3.1.1", "https://cran.r-project.org/web/packages/AER/index.html", 13 [ 14 Package("car", "3.0-10", "https://cran.r-project.org/web/packages/car/index.html", []), 15 Package("effects", "4.2-0","https://cran.r-project.org/web/packages/effects/index.html", []), 16 Package("foreign", "0.8-80", "https://cran.r-project.org/web/packages/foreign/index.html", []), 17 ] 18) 19 20package.print() 21``` 22 23 Package: 24 name: AER 25 version: 3.1.1 26 url: https://cran.r-project.org/web/packages/AER/index.html 27 dependencies: 28 car:3.0-10 29 effects:4.2-0 30 foreign:0.8-80 31 32 33 34```python 35package.to_dict() 36``` 37 38 {'name': 'AER', 39 'version': '3.1.1', 40 'url': 'https://cran.r-project.org/web/packages/AER/index.html', 41 'dependencies': [{'name': 'car', 42 'version': '3.0-10', 43 'url': 'https://cran.r-project.org/web/packages/car/index.html', 44 'dependencies': []}, 45 {'name': 'effects', 46 'version': '4.2-0', 47 'url': 'https://cran.r-project.org/web/packages/effects/index.html', 48 'dependencies': []}, 49 {'name': 'foreign', 50 'version': '0.8-80', 51 'url': 'https://cran.r-project.org/web/packages/foreign/index.html', 52 'dependencies': []}]} 53 54 55 56 57```python 58package.load( 59 { 60 "name": "AER", 61 "version": "3.1.1", 62 "url": "https://cran.r-project.org/web/packages/AER/index.html", 63 "dependencies": [ 64 { 65 "name": "car", 66 "version": "3.0-10", 67 "url": "https://cran.r-project.org/web/packages/car/index.html", 68 "dependencies": [] 69 }, 70 { 71 "name": "effects", 72 "version": "4.2-0", 73 "url": "https://cran.r-project.org/web/packages/effects/index.html", 74 "dependencies": [] 75 }, 76 { 77 "name": "foreign", 78 "version": "0.8-80", 79 "url": "https://cran.r-project.org/web/packages/foreign/index.html", 80 "dependencies": [] 81 } 82 ] 83 } 84 85) 86``` 87 88 89 90 91 <olivia_finder.package.Package at 0x7f3c2eb5c2e0> 92 93""" 94 95from __future__ import annotations 96from typing import List, Optional 97 98class Package: 99 ''' 100 Class that represents a package in the network 101 ''' 102 103 def __init__( 104 self, 105 name: str, 106 version: Optional[str] = None, 107 url: Optional[str] = None, 108 dependencies: Optional[List[Package]] = None 109 ): 110 ''' 111 Constructor of the class 112 113 Parameters 114 ---------- 115 name : str 116 Name of the package 117 version : Optional[str], optional 118 Version of the package, by default None 119 url : Optional[str], optional 120 Url of the package, by default None 121 dependencies : Optional[List[Package]], optional 122 List of dependencies of the package, by default None 123 124 Examples 125 -------- 126 >>> from olivia_finder.package import Package 127 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 128 129 ''' 130 131 self.name: str = name 132 self.version: Optional[str] = version 133 self.url: Optional[str] = url 134 self.dependencies: List[Package] = [] if dependencies is None else dependencies 135 136 def print(self): 137 ''' 138 Print the package data in the console 139 140 Examples 141 -------- 142 >>> from olivia_finder.package import Package 143 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 144 >>> package.print() 145 146 Package: 147 name: package 148 version: 1.0.0 149 url: https://package.org 150 dependencies: 151 dependency1:1.0.0 152 dependency2:1.0.0 153 ''' 154 print("Package:") 155 print(f" name: {self.name}") 156 print(f" version: {self.version}") 157 print(f" url: {self.url}") 158 print(" dependencies:") 159 for dependency in self.dependencies: 160 print(f" {str(dependency)}") 161 162 def __eq__(self, other) -> bool: 163 ''' 164 Compare two packages for equality 165 166 Parameters 167 ---------- 168 other : Package 169 Package to compare with 170 171 Returns 172 ------- 173 bool 174 True if the packages are equal, False otherwise 175 176 Examples 177 -------- 178 >>> from olivia_finder.package import Package 179 >>> package1 = Package("numpy", "1.0.0", "https://numpy.org") 180 >>> package2 = Package("numpy", "1.0.0", "https://numpy.org") 181 >>> package1 == package2 182 ''' 183 return self.name == other.name and self.version == other.version 184 185 def __hash__(self) -> int: 186 ''' 187 Hash code of the package 188 189 Returns 190 ------- 191 int 192 Hash code of the package 193 194 Examples 195 -------- 196 >>> from olivia_finder.package import Package 197 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 198 >>> hash(package) 199 ''' 200 201 hash_code = 0 202 if self.name is not None: 203 hash_code += hash(self.name) 204 if self.version is not None: 205 hash_code += hash(self.version) 206 if self.url is not None: 207 hash_code += hash(self.url) 208 if self.dependencies is not None: 209 hash_code += hash(tuple(self.dependencies)) 210 return hash_code 211 212 def __str__(self) -> str: 213 ''' 214 String representation of the package 215 216 Returns 217 ------- 218 str 219 String representation of the package 220 221 Examples 222 -------- 223 >>> from olivia_finder.package import Package 224 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 225 >>> str(package) 226 numpy:1.0.0 227 ''' 228 return self.name if self.version is None else f"{self.name}:{self.version}" 229 230 def update(self, data: dict): 231 ''' 232 Update the package with the data of a dictionary. 233 Only the attributes version, url and dependencies are supported. 234 235 Parameters 236 ---------- 237 data : dict 238 dictionary with the data to update 239 240 Examples 241 -------- 242 >>> from olivia_finder.package import Package 243 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 244 >>> package.update({"version": "1.0.1"}) 245 ''' 246 247 if 'version' in data: 248 self.version = data['version'] 249 if 'url' in data: 250 self.url = data['url'] 251 if 'dependencies' in data: 252 self.dependencies = list(set(data['dependencies'])) 253 254 def to_dict(self): 255 ''' 256 Convert the package to a dictionary with the following structure: 257 { 258 'name': str, 259 'version': str, 260 'url': str, 261 'dependencies': List[dict] 262 } 263 264 Returns 265 ------- 266 dict 267 dictionary with the data of the package 268 269 Examples 270 -------- 271 >>> from olivia_finder.package import Package 272 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 273 >>> package.to_dict() 274 ''' 275 276 # Create the dictionary 277 data = { 278 'name': self.name, 279 'version': self.version, 280 'url': self.url, 281 'dependencies': [] 282 } 283 284 # Add the dependencies as a dictionary 285 for dependency in self.dependencies: 286 data['dependencies'].append(dependency.to_dict()) 287 288 return data 289 290 @classmethod 291 def load(cls, data: dict): 292 ''' 293 Loads a package from a dictionary. 294 It is assumed that the dictionary has the following structure:: 295 296 { 297 'name': str, 298 'version': str, 299 'url': str, 300 'dependencies': List[dict] 301 } 302 303 Parameters 304 ---------- 305 data : dict 306 dictionary with the data 307 308 Returns 309 ------- 310 Package 311 Package loaded from the dictionary 312 313 Examples 314 -------- 315 >>> from olivia_finder.package import Package 316 >>> package = Package.load( 317 {"name": "numpy", "version": "1.0.0", "url": "https://numpy.org"} 318 ) 319 ''' 320 dependencies = [ 321 Package(dependency['name'], dependency['version']) 322 for dependency in data['dependencies'] 323 ] 324 return cls(data['name'], data['version'], data['url'], dependencies) 325 326 def get_dependencies(self) -> List[Package]: 327 ''' 328 Get the dependencies of the package or an empty list if there are no dependencies 329 330 Returns 331 ------- 332 List[Package] 333 List with the dependencies 334 335 Examples 336 -------- 337 >>> from olivia_finder.package import Package 338 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 339 >>> package.get_dependencies() 340 ''' 341 return self.dependencies 342 343 def get_dependencies_names(self) -> List[str]: 344 ''' 345 Get the names of the dependencies of the package or an empty list if there are no dependencies 346 347 Returns 348 ------- 349 List[str] 350 List with the names of the dependencies 351 352 Examples 353 -------- 354 >>> from olivia_finder.package import Package 355 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 356 >>> package.get_dependencies_names() 357 ''' 358 return [dependency.name for dependency in self.dependencies]
99class Package: 100 ''' 101 Class that represents a package in the network 102 ''' 103 104 def __init__( 105 self, 106 name: str, 107 version: Optional[str] = None, 108 url: Optional[str] = None, 109 dependencies: Optional[List[Package]] = None 110 ): 111 ''' 112 Constructor of the class 113 114 Parameters 115 ---------- 116 name : str 117 Name of the package 118 version : Optional[str], optional 119 Version of the package, by default None 120 url : Optional[str], optional 121 Url of the package, by default None 122 dependencies : Optional[List[Package]], optional 123 List of dependencies of the package, by default None 124 125 Examples 126 -------- 127 >>> from olivia_finder.package import Package 128 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 129 130 ''' 131 132 self.name: str = name 133 self.version: Optional[str] = version 134 self.url: Optional[str] = url 135 self.dependencies: List[Package] = [] if dependencies is None else dependencies 136 137 def print(self): 138 ''' 139 Print the package data in the console 140 141 Examples 142 -------- 143 >>> from olivia_finder.package import Package 144 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 145 >>> package.print() 146 147 Package: 148 name: package 149 version: 1.0.0 150 url: https://package.org 151 dependencies: 152 dependency1:1.0.0 153 dependency2:1.0.0 154 ''' 155 print("Package:") 156 print(f" name: {self.name}") 157 print(f" version: {self.version}") 158 print(f" url: {self.url}") 159 print(" dependencies:") 160 for dependency in self.dependencies: 161 print(f" {str(dependency)}") 162 163 def __eq__(self, other) -> bool: 164 ''' 165 Compare two packages for equality 166 167 Parameters 168 ---------- 169 other : Package 170 Package to compare with 171 172 Returns 173 ------- 174 bool 175 True if the packages are equal, False otherwise 176 177 Examples 178 -------- 179 >>> from olivia_finder.package import Package 180 >>> package1 = Package("numpy", "1.0.0", "https://numpy.org") 181 >>> package2 = Package("numpy", "1.0.0", "https://numpy.org") 182 >>> package1 == package2 183 ''' 184 return self.name == other.name and self.version == other.version 185 186 def __hash__(self) -> int: 187 ''' 188 Hash code of the package 189 190 Returns 191 ------- 192 int 193 Hash code of the package 194 195 Examples 196 -------- 197 >>> from olivia_finder.package import Package 198 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 199 >>> hash(package) 200 ''' 201 202 hash_code = 0 203 if self.name is not None: 204 hash_code += hash(self.name) 205 if self.version is not None: 206 hash_code += hash(self.version) 207 if self.url is not None: 208 hash_code += hash(self.url) 209 if self.dependencies is not None: 210 hash_code += hash(tuple(self.dependencies)) 211 return hash_code 212 213 def __str__(self) -> str: 214 ''' 215 String representation of the package 216 217 Returns 218 ------- 219 str 220 String representation of the package 221 222 Examples 223 -------- 224 >>> from olivia_finder.package import Package 225 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 226 >>> str(package) 227 numpy:1.0.0 228 ''' 229 return self.name if self.version is None else f"{self.name}:{self.version}" 230 231 def update(self, data: dict): 232 ''' 233 Update the package with the data of a dictionary. 234 Only the attributes version, url and dependencies are supported. 235 236 Parameters 237 ---------- 238 data : dict 239 dictionary with the data to update 240 241 Examples 242 -------- 243 >>> from olivia_finder.package import Package 244 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 245 >>> package.update({"version": "1.0.1"}) 246 ''' 247 248 if 'version' in data: 249 self.version = data['version'] 250 if 'url' in data: 251 self.url = data['url'] 252 if 'dependencies' in data: 253 self.dependencies = list(set(data['dependencies'])) 254 255 def to_dict(self): 256 ''' 257 Convert the package to a dictionary with the following structure: 258 { 259 'name': str, 260 'version': str, 261 'url': str, 262 'dependencies': List[dict] 263 } 264 265 Returns 266 ------- 267 dict 268 dictionary with the data of the package 269 270 Examples 271 -------- 272 >>> from olivia_finder.package import Package 273 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 274 >>> package.to_dict() 275 ''' 276 277 # Create the dictionary 278 data = { 279 'name': self.name, 280 'version': self.version, 281 'url': self.url, 282 'dependencies': [] 283 } 284 285 # Add the dependencies as a dictionary 286 for dependency in self.dependencies: 287 data['dependencies'].append(dependency.to_dict()) 288 289 return data 290 291 @classmethod 292 def load(cls, data: dict): 293 ''' 294 Loads a package from a dictionary. 295 It is assumed that the dictionary has the following structure:: 296 297 { 298 'name': str, 299 'version': str, 300 'url': str, 301 'dependencies': List[dict] 302 } 303 304 Parameters 305 ---------- 306 data : dict 307 dictionary with the data 308 309 Returns 310 ------- 311 Package 312 Package loaded from the dictionary 313 314 Examples 315 -------- 316 >>> from olivia_finder.package import Package 317 >>> package = Package.load( 318 {"name": "numpy", "version": "1.0.0", "url": "https://numpy.org"} 319 ) 320 ''' 321 dependencies = [ 322 Package(dependency['name'], dependency['version']) 323 for dependency in data['dependencies'] 324 ] 325 return cls(data['name'], data['version'], data['url'], dependencies) 326 327 def get_dependencies(self) -> List[Package]: 328 ''' 329 Get the dependencies of the package or an empty list if there are no dependencies 330 331 Returns 332 ------- 333 List[Package] 334 List with the dependencies 335 336 Examples 337 -------- 338 >>> from olivia_finder.package import Package 339 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 340 >>> package.get_dependencies() 341 ''' 342 return self.dependencies 343 344 def get_dependencies_names(self) -> List[str]: 345 ''' 346 Get the names of the dependencies of the package or an empty list if there are no dependencies 347 348 Returns 349 ------- 350 List[str] 351 List with the names of the dependencies 352 353 Examples 354 -------- 355 >>> from olivia_finder.package import Package 356 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 357 >>> package.get_dependencies_names() 358 ''' 359 return [dependency.name for dependency in self.dependencies]
Class that represents a package in the network
104 def __init__( 105 self, 106 name: str, 107 version: Optional[str] = None, 108 url: Optional[str] = None, 109 dependencies: Optional[List[Package]] = None 110 ): 111 ''' 112 Constructor of the class 113 114 Parameters 115 ---------- 116 name : str 117 Name of the package 118 version : Optional[str], optional 119 Version of the package, by default None 120 url : Optional[str], optional 121 Url of the package, by default None 122 dependencies : Optional[List[Package]], optional 123 List of dependencies of the package, by default None 124 125 Examples 126 -------- 127 >>> from olivia_finder.package import Package 128 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 129 130 ''' 131 132 self.name: str = name 133 self.version: Optional[str] = version 134 self.url: Optional[str] = url 135 self.dependencies: List[Package] = [] if dependencies is None else dependencies
Constructor of the class
Parameters
- name (str): Name of the package
- version (Optional[str], optional): Version of the package, by default None
- url (Optional[str], optional): Url of the package, by default None
- dependencies (Optional[List[Package]], optional): List of dependencies of the package, by default None
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
137 def print(self): 138 ''' 139 Print the package data in the console 140 141 Examples 142 -------- 143 >>> from olivia_finder.package import Package 144 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 145 >>> package.print() 146 147 Package: 148 name: package 149 version: 1.0.0 150 url: https://package.org 151 dependencies: 152 dependency1:1.0.0 153 dependency2:1.0.0 154 ''' 155 print("Package:") 156 print(f" name: {self.name}") 157 print(f" version: {self.version}") 158 print(f" url: {self.url}") 159 print(" dependencies:") 160 for dependency in self.dependencies: 161 print(f" {str(dependency)}")
Print the package data in the console
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
>>> package.print()
Package: name: package version: 1.0.0 url: https://package.org dependencies: dependency1:1.0.0 dependency2:1.0.0
231 def update(self, data: dict): 232 ''' 233 Update the package with the data of a dictionary. 234 Only the attributes version, url and dependencies are supported. 235 236 Parameters 237 ---------- 238 data : dict 239 dictionary with the data to update 240 241 Examples 242 -------- 243 >>> from olivia_finder.package import Package 244 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 245 >>> package.update({"version": "1.0.1"}) 246 ''' 247 248 if 'version' in data: 249 self.version = data['version'] 250 if 'url' in data: 251 self.url = data['url'] 252 if 'dependencies' in data: 253 self.dependencies = list(set(data['dependencies']))
Update the package with the data of a dictionary. Only the attributes version, url and dependencies are supported.
Parameters
- data (dict): dictionary with the data to update
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
>>> package.update({"version": "1.0.1"})
255 def to_dict(self): 256 ''' 257 Convert the package to a dictionary with the following structure: 258 { 259 'name': str, 260 'version': str, 261 'url': str, 262 'dependencies': List[dict] 263 } 264 265 Returns 266 ------- 267 dict 268 dictionary with the data of the package 269 270 Examples 271 -------- 272 >>> from olivia_finder.package import Package 273 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 274 >>> package.to_dict() 275 ''' 276 277 # Create the dictionary 278 data = { 279 'name': self.name, 280 'version': self.version, 281 'url': self.url, 282 'dependencies': [] 283 } 284 285 # Add the dependencies as a dictionary 286 for dependency in self.dependencies: 287 data['dependencies'].append(dependency.to_dict()) 288 289 return data
Convert the package to a dictionary with the following structure: { 'name': str, 'version': str, 'url': str, 'dependencies': List[dict] }
Returns
- dict: dictionary with the data of the package
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
>>> package.to_dict()
291 @classmethod 292 def load(cls, data: dict): 293 ''' 294 Loads a package from a dictionary. 295 It is assumed that the dictionary has the following structure:: 296 297 { 298 'name': str, 299 'version': str, 300 'url': str, 301 'dependencies': List[dict] 302 } 303 304 Parameters 305 ---------- 306 data : dict 307 dictionary with the data 308 309 Returns 310 ------- 311 Package 312 Package loaded from the dictionary 313 314 Examples 315 -------- 316 >>> from olivia_finder.package import Package 317 >>> package = Package.load( 318 {"name": "numpy", "version": "1.0.0", "url": "https://numpy.org"} 319 ) 320 ''' 321 dependencies = [ 322 Package(dependency['name'], dependency['version']) 323 for dependency in data['dependencies'] 324 ] 325 return cls(data['name'], data['version'], data['url'], dependencies)
Loads a package from a dictionary. It is assumed that the dictionary has the following structure::
{
'name': str,
'version': str,
'url': str,
'dependencies': List[dict]
}
Parameters
- data (dict): dictionary with the data
Returns
- Package: Package loaded from the dictionary
Examples
>>> from olivia_finder.package import Package
>>> package = Package.load(
{"name": "numpy", "version": "1.0.0", "url": "https://numpy.org"}
)
327 def get_dependencies(self) -> List[Package]: 328 ''' 329 Get the dependencies of the package or an empty list if there are no dependencies 330 331 Returns 332 ------- 333 List[Package] 334 List with the dependencies 335 336 Examples 337 -------- 338 >>> from olivia_finder.package import Package 339 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 340 >>> package.get_dependencies() 341 ''' 342 return self.dependencies
Get the dependencies of the package or an empty list if there are no dependencies
Returns
- List[Package]: List with the dependencies
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
>>> package.get_dependencies()
344 def get_dependencies_names(self) -> List[str]: 345 ''' 346 Get the names of the dependencies of the package or an empty list if there are no dependencies 347 348 Returns 349 ------- 350 List[str] 351 List with the names of the dependencies 352 353 Examples 354 -------- 355 >>> from olivia_finder.package import Package 356 >>> package = Package("numpy", "1.0.0", "https://numpy.org") 357 >>> package.get_dependencies_names() 358 ''' 359 return [dependency.name for dependency in self.dependencies]
Get the names of the dependencies of the package or an empty list if there are no dependencies
Returns
- List[str]: List with the names of the dependencies
Examples
>>> from olivia_finder.package import Package
>>> package = Package("numpy", "1.0.0", "https://numpy.org")
>>> package.get_dependencies_names()