Explore all Programming Style open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Programming Style

axios

v0.26.1

RxJava

3.1.4

jadx

1.3.5

fetch

RxSwift

Atlas

Popular Libraries in Programming Style

axios

by axios doticonjavascriptdoticon

star image 92140 doticonMIT

Promise based HTTP client for the browser and node.js

RxJava

by ReactiveX doticonjavadoticon

star image 45971 doticonApache-2.0

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

jadx

by skylot doticonjavadoticon

star image 29830 doticonApache-2.0

Dex to Java decompiler

async

by caolan doticonjavascriptdoticon

star image 27509 doticonMIT

Async utilities for node and the browser

rxjs

by ReactiveX doticontypescriptdoticon

star image 26586 doticonApache-2.0

A reactive programming library for JavaScript

fetch

by github doticonjavascriptdoticon

star image 25051 doticonMIT

A window.fetch JavaScript polyfill.

RxSwift

by ReactiveX doticonswiftdoticon

star image 22027 doticonNOASSERTION

Reactive Programming in Swift

ramda

by ramda doticonjavascriptdoticon

star image 21915 doticonMIT

:ram: Practical functional Javascript

rollup

by rollup doticonjavascriptdoticon

star image 21476 doticonNOASSERTION

Next-generation ES module bundler

Trending New libraries in Programming Style

MudBlazor

by MudBlazor doticoncsharpdoticon

star image 2931 doticonMIT

Blazor Component Library based on Material design. The goal is to do more with Blazor, utilizing CSS and keeping Javascript to a bare minimum.

ts-pattern

by gvergnaud doticontypescriptdoticon

star image 2569 doticonMIT

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

piscina

by piscinajs doticontypescriptdoticon

star image 2414 doticonNOASSERTION

A fast, efficient Node.js Worker Thread Pool implementation

win10script

by ChrisTitusTech doticonpowershelldoticon

star image 2410 doticonMIT

This is the Ultimate Windows 10 Script from a creation from multiple debloat scripts and gists from github.

lunatic

by lunatic-solutions doticonrustdoticon

star image 2249 doticonNOASSERTION

Lunatic is an Erlang-inspired runtime for WebAssembly

smol

by smol-rs doticonrustdoticon

star image 2074 doticonNOASSERTION

A small and fast async runtime for Rust

small-spring

by fuzhengwei doticonjavadoticon

star image 1925 doticonApache-2.0

🌱《 Spring 手撸专栏》,本专栏以 Spring 源码学习为目的,通过手写简化版 Spring 框架,了解 Spring 核心原理。在手写的过程中会简化 Spring 源码,摘取整体框架中的核心逻辑,简化代码实现过程,保留核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等内容实现。

magic-python

by iswbm doticonpythondoticon

star image 1913 doticon

Python 黑魔法手册

introduction-to-bash-scripting

by bobbyiliev doticonhtmldoticon

star image 1854 doticonMIT

Free Introduction to Bash Scripting eBook

Top Authors in Programming Style

1

PacktPublishing

98 Libraries

star icon2234

2

sindresorhus

62 Libraries

star icon12959

3

es-shims

49 Libraries

star icon7836

4

aio-libs

36 Libraries

star icon22896

5

LuaDist

36 Libraries

star icon2616

6

HowProgrammingWorks

34 Libraries

star icon250

7

akarnokd

28 Libraries

star icon1933

8

vert-x

26 Libraries

star icon1089

9

yoshuawuyts

24 Libraries

star icon490

10

vert-x3

23 Libraries

star icon5125

1

98 Libraries

star icon2234

2

62 Libraries

star icon12959

3

49 Libraries

star icon7836

4

36 Libraries

star icon22896

5

36 Libraries

star icon2616

6

34 Libraries

star icon250

7

28 Libraries

star icon1933

8

26 Libraries

star icon1089

9

24 Libraries

star icon490

10

23 Libraries

star icon5125

Trending Kits in Programming Style

Python encryption libraries provide base chunks of pre-written code that can be repurposed to develop a unique encryption-decryption system.

These libraries offer a long list of primitives a developer can build upon, choosing from cipher-decipher algorithms like AES, RSA, DES, etc. It allows developers to deal with sideline attacks better. Open-source Python libraries, not being a part of the standard package, can be installed using the PIP function. Python encryptions systems are not web-exclusive; the language allows a developer the flexibility of cross-platform use, unlike other popular coding languages like, say, PHP.


The list below summarizes our top open-source python libraries, consisting of ready-to-incorporate code components for designing encrypted security. Certbot acquires SSL certificates from the open-source certificate authority, Let's Encrypt. It also gives the developer the option to automatically enable HTTPS protocol and to act as a client for certificate authorities running on the ACME protocol. Mailpile, a web-mail client, focuses on the overall experience by providing a clean user interface. While being a web-based interface, it also provides an API and a command-line interface for developers. Ciphey employs artificial intelligence to assess the type of encryption and decipher the input text fast. It is minimalistic and precise.

certbot:  

  • It is a command-line tool for managing SSL/TLS certificates.   
  • It is often used in conjunction with Python web servers, such as Nginx or Apache.   
  • It enables secure communication over HTTPS.  

Ciphey:  

  • Ciphey is a Python library used in Institutions, Education, Security, and Cryptography applications.  
  • It is a tool designed for automatic decryption of ciphers and codes.  
  • It aims to simplify the process of deciphering encrypted messages. This detects the encryption method and provides the decrypted result.  

Mailpile:  

  • Mailpile helps in Institutions, Learning, Administration, Public Services, Messaging, and Email applications.  
  • It provides an interface for managing and encrypting emails.  
  • Its primary user interface is web-based. It also offers a basic command-line interface and an API for developers.

byob:  

  • BYOB in Python generally refers to "Bring Your Own Bytes" or "Bring Your Own Key," depending on the context.  
  • It allows users to provide their own cryptographic keys. Rather than relying on default or generated keys.  
  • BYOB enables customization to meet these needs.

cryptography:  

  • It is a Python library used in Security, Cryptography applications.  
  • It exposes cryptographic primitives and recipes to Python developers.  
  • It ensures data confidentiality, integrity, and authenticity.

acme-tiny:  

  • acme-tiny is a Python encryption library.  
  • It helps with Security, Encryption, and Docker applications.  
  • You can install using 'pip install acme-tiny' or download it from GitHub, PyPI. It is used as a tiny script to issue and renew TLS certs from Let's Encrypt.

yadm:  

  • yadm is a Python library used in Devops, Configuration Management applications.  
  • yadm is a tool for managing dotfiles.  
  • It helps ensure consistency and ease of setup by keeping track of configurations.

ssh-audit:  

  • ssh-audit is a Python library.  
  • It is a tool used to audit the security configurations of SSH servers.  
  • It identifies potential vulnerabilities and weaknesses in the SSH configuration.

PyBitmessage:  

  • PyBitmessage is a Python library used in Telecommunications, Media, Telecom, Networking applications.  
  • It is a P2P communication protocol used to send encrypted messages to another person.  
  • It aims to hide metadata from passive eavesdroppers.

RsaCtfTool:  

  • RsaCtfTool is a Python library used in Security and Cryptography applications.  
  • It is a Python-based tool designed for solving RSA Capture the Flag (CTF) challenges.  
  • It plays a crucial role in CTF competitions. Its participants often encounter RSA-related problems. 

pycrypto:  

  • PyCrypto is important for several reasons in the context of encryption.  
  • PyCrypto supports various encryption algorithms, hashing functions, and random number generators.  
  • PyCrypto facilitates interoperability by supporting used cryptographic standards.

EQGRP_Lost_in_Translation:  

  • EQGRP_Lost_in_Translation is a Python library.   
  • It helps in Programming Style applications.  
  • It decrypts content of odd.tar.xz.gpg, swift.tar.xz.gpg and windows.tar.xz.gpg.

asyncssh:  

  • asyncssh is a Python library that provides an asynchronous framework for SSH communication.  
  • SSH relies on encryption algorithms to secure data transmission.  
  • asyncssh supports various encryption algorithms, providing a secure means of communication over networks.

Cloakify:  

  • Cloakify is a Python library used in Testing and Security Testing applications.  
  • It is a tool designed to obfuscate or "cloak" data in various formats, making it less conspicuous.  
  • This is useful for hiding sensitive information in plain sight.

demiguise:  

  • demiguise is a Python encryption library.  
  • It helps in Security, Encryption applications.  
  • It is an HTA encryption tool for RedTeams.

Crypton:  

  • Crypton is a Python library used in Security, Cryptography applications.  
  • Crypton is an educational library to learn and practice Offensive and Defensive Cryptography.  
  • It is an explanation of all the existing vulnerabilities on various Systems.

xortool:  

  • It is a tool used for analyzing and breaking simple XOR-based encryption.  
  • XOR is a bitwise operation that helps in encryption.  
  • It is a tool used to analyze multi-byte xor cipher.

tf-encrypted:  

  • tf-encrypted is a Python library that extends TensorFlow.   
  • TansorFlow extends to enable privacy-preserving machine learning using encrypted data.  
  • It aims to make privacy-preserving machine learning available, without requiring expertise in cryptography.

GlobaLeaks:  

  • GlobaLeaks is a Python library used in Security, Encryption applications.  
  • It is an open-source whistleblowing framework designed for secure and anonymous communication.  
  • It provides tools for organizations to set up their own secure whistleblowing platforms.

server:  

  • servers enable secure connections, like HTTPS. HTTPs are vital for protecting sensitive information during data transmission over networks.  
  • It ensures the confidentiality and integrity of data by handling encryption keys.  
  • It provides a central point for managing cryptographic operations.

ssl_logger:  

  • ssl_logger is a Python library used in Security, TLS applications.  
  • It helps in identifying potential vulnerabilities, debugging handshake problems, and ensuring secure communication.  
  • It Decrypts and logs a process's SSL traffic.

simp_le:  

  • simp_le is a Python library that helps with encryption.  
  • It Encrypts Client. It has no bugs and has no vulnerabilities.  
  • simp_le can download it from GitHub.

featherduster:  

  • FeatherDuster is a Python library designed for educational purposes.  
  • It is to help users understand various aspects of cryptography.   
  • It helps in penetration testing scenarios. It assesses the security of cryptographic components in apps and systems.

hawkpost:  

  • featherduster is a Python library used in Security, Cryptography applications.  
  • It is an online service that allows users to create encrypted messages with a sharable link.  
  • Cryptanalib is the moving part behind FeatherDuster, and helps with FeatherDuster.

tfc:  

  • tfc is a Python library used in Networking, Router applications.  
  • It helps developers install privacy-preserving machine-learning techniques.   
  • It is a Tinfoil Chat - Onion-routed, endpoint secure messaging system.

pyopenssl:  

  • pyOpenSSL is a Python wrapper around the OpenSSL library.   
  • It provides support for secure sockets (SSL/TLS) and cryptographic functions.  
  • It allows Python apps to establish secure connections over the internet. It uses the SSL/TLS protocol.

nucypher:  

  • NuCypher is provides a decentralized key management system.  
  • It allows for proxy re-encryption, enabling data sharing without exposing sensitive keys.  
  • This is valuable for apps requiring secure and decentralized access control in blockchain. 

RAASNet:  

  • RAASNet is a Python Encryption library.  
  • It helps with Testing and Security Testing applications.  
  • It is an Open-Source Ransomware as a Service for Linux, MacOS and Windows.

dnsrobocert:  

  • dnsrobocert is a Python library used in Security, TLS, Docker applications.  
  • It obtains SSL/TLS certificates through an automated process.   
  • It integrates with DNS challenges for verification.

Xeexe-TopAntivirusEvasion:  

  • Xeexe-TopAntivirusEvasion is a Python library used in Security, Firewall applications.  
  • It is an Undetectable & Xor encrypting with custom KEY.  
  • It bypasses Top Antivirus like BitDefender, Malwarebytes, Avast, ESET-NOD32, AVG, & Add ICON and MANIFEST to excitable.

Decentralized-Internet:  

  • A decentralized internet can enhance security in Python encryption libraries. It reduces the reliance on central authorities.  
  • This enhances the robustness of encryption implementations.  
  • It can contribute to user privacy by minimizing the collection of sensitive data.

PacketWhisper:  

  • PacketWhisper is a Python library used in Testing, Security Testing applications.  
  • PacketWhisper helps to address specific needs or vulnerabilities in network communication.  
  • It could be valuable for scenarios where secure packet transmission is crucial.

python-paillier:  

  • Python-Paillier is a library that implements the Paillier cryptosystem in Python.  
  • In machine learning, Python-Paillier applies to build privacy-preserving models.  
  • Python-Paillier, being an open-source library, encourages collaboration and contributions from the community.

covertutils:  

  • covertutils is a Python encryption library   
  • It helps with Testing and Security Testing applications.  
  • It is a framework for Backdoor development.

decrypt:  

  • It is crucial for retrieving original data from encrypted content.  
  • It ensures data confidentiality. It allows authorized users to access and understand the information.  
  • It is essential in scenarios were sensitive data needs transmission or storage.

nufhe:  

  • nufhe is a Python library used in Security, Encryption applications.  
  • It is a NuCypher homomorphic encryption (NuFHE) library implemented in Python.  
  • You can install using 'pip install nufhe' or download it from GitHub, PyPI.  

rsa-wiener-attack:  

  • rsa-wiener-attack is a Python library used in Security, Cryptography applications.  
  • A Python version of the Wiener attack targeting the RSA public-key encryption system.  
  • It targets cases where the private exponent is small. It allows an attacker to factorize the modulus.

an2linuxserver:  

  • an2linuxserver is a Python encryption library.  
  • It helps in Security, Encryption applications.  
  • It is a Sync Android notification encrypted to a Linux desktop.

python-rsa:  

  • It provides functionality for working with RSA encryption, a used public-key cryptosystem.  
  • python-rsa helps ensure the confidentiality and integrity of data during transmission.  
  • It provides tools for managing RSA keys, including key generation, serialization, and storage.

NXcrypt:  

  • NXcrypt is a Python library used in Artificial Intelligence, Machine Learning applications.  
  • It is a polymorphic 'python backdoors' crypter written in python by Hadi Mene (h4d3s).  
  • NXcrypt can inject malicious Python files into a normal file using a multi-threading system.

gpgsync:  

  • GPG in Python, it's crucial for key management, encryption, and digital signatures.  
  • GPG provides a way to secure communication and data integrity.   
  • It can enhance the security of your apps. It helps in dealing with sensitive information.

ShellcodeWrapper:  

  • ShellcodeWrapper is a Python encryption library, used in Security and Hacking applications.  
  • Wrappers help organize code by encapsulating related functionalities.  
  • Wrappers often serve as a convenient interface or encapsulation for underlying functionality.

nfreezer:  

  • nfreezer is a Python library used in Security, Encryption applications.  
  • nFreezer (for encrypted freezer) is an encrypted-at-rest backup tool.  
  • It helps in the cases with untrusted destination servers.

oscrypto:  

  • oscrypto is a Python library that provides a high-level interface to cryptographic operations.  
  • It is built on top of the cryptography library. It aims to simplify the use of cryptographic functions in Python.  
  • Its ability to offer a consistent API for various cryptographic tasks.

encrypted-dns:  

  • Encrypted DNS (Domain Name System) in Python encryption libraries.  
  • It is crucial for enhancing the security and privacy of internet communication.  
  • It integrated with encrypted DNS. It ensures the process of resolving domain names to IP addresses is secure.

simple-crypt:  

  • It provides a simple interface for symmetric encryption and decryption.  
  • It can serve as an educational tool. This tool helps individuals who are learning about encryption.  
  • It allows developers to install basic encryption. It enables them to focus on other aspects of their projects.

privy:  

  • privy is a Python encryption library.  
  • It helps in Security, Encryption applications.  
  • It is an easy, fast lib to password-protect your data. 

FAQ 

1.What is encryption?  

It is the process of converting plaintext data into a secure and unreadable form. It is also known as ciphertext, to protect sensitive information.  

 

2.Why should I use encryption in Python?  

Encryption helps secure data during transmission or storage, preventing unauthorized access. It's crucial for protecting sensitive information like passwords, personal data, or confidential files.  

 

3.Which encryption libraries to use in Python?  

Popular encryption libraries in Python include cryptography, PyCryptodome, and cryptography. This library provides high-level cryptographic primitives.  

 

4.How do I install a Python encryption library?  

You can install most libraries using a package manager like pip. For example, to install the cryptography library, run pip install cryptography.  

 

5.What types of encryption algorithms are supported?  

Python encryption libraries often support various algorithms. It includes AES (Advanced Encryption Standard), RSA (Rivest-Shamir-Adleman), and others. Check the documentation for the specific library to see which algorithms are supported.

Here are some of the famous Python WebSocket Utilities Libraries. Some use cases of Python WebSocket Utilities Libraries include Real-time Chat and Messaging Applications, Online Gaming, IoT Applications, and Real-time Data Visualization and Dashboards.


Python WebSocket utilities libraries are collections of code that provide a set of utilities to help developers create and manage WebSocket connections in Python. These libraries typically provide methods to simplify WebSocket connection setup, message sending, message receiving and connection management. They can also provide additional features such as authentication and SSL/TLS support.


Let us have a look at some of these libraries in detail below.

tornado

  • Can handle up to 10,000 simultaneous open connections, making it ideal for applications with high levels of concurrent users.
  • Can handle multiple requests simultaneously without blocking requests.
  • One of the few web frameworks that supports WebSocket connections.

gevent

  • Based on greenlet and libevent, making it extremely fast, lightweight and efficient.
  • Highly extensible and can be easily integrated with other Python libraries and frameworks.
  • Provides a high level of concurrency, allowing multiple requests to be handled at the same time.

twisted

  • Event-driven architecture makes it easy to build highly concurrent applications.
  • Can be used to build distributed applications, which can be used to connect multiple machines over the network.
  • Provides a low-level interface which makes it easier to work with websockets.

websockets

  • Data can be sent and received quickly, allowing for real-time communication.
  • Enable bidirectional communication between the client and the server.
  • Use the secure websocket protocol (WSS) which encrypts all data sent over the connection.

websocket-client

  • Built on top of the standard library's asyncio module, which allows for asynchronous communication with websockets.
  • Supports secure websocket connections via TLS/SSL, as well as binary messages and fragmented messages.
  • Supports custom headers and subprotocols, making it easy to communicate with specific services that require specific headers or subprotocols.

WebSocket-for-Python

  • Supports multiple protocols such as WebSocket, HTTP, and TCP, allowing for more flexible usage.
  • Has built-in security features such as authentication and encryption, allowing you to securely communicate with other applications.
  • Is written in Python, making it easy to use and integrate with existing Python applications.

socketIO-client

  • Supports multiple transports, including long polling, WebSockets and cross-browser support.
  • Support for namespaces, allowing for multiple independent connections to the same server.
  • Allows for subscribing to multiple events, allowing for a more efficient implementation of your application.

pywebsocket

  • Supports both server-side and client-side websocket connections.
  • Provides support for websocket extensions.
  • Supports both connection-oriented and connectionless websockets, making it a versatile tool for developers.

Here are some of the famous Node JS HTTP Request Libraries. The use cases of these libraries include Server-side Web Apps, Mobile App Development, Data Processing, Data Analysis, and Network Monitoring.


Node.js HTTP Request Libraries are libraries that allow Node.js developers to make requests to web servers in order to retrieve data. This can be helpful when building web applications that need to pull in external data from other web servers. Examples of popular Node.js HTTP Request Libraries are Axios, Request, and Superagent.


Let us look at some of these famous libraries. 

axios

  • Automatically transforms all request and response data into JSON, making it easier to handle data.
  • Supports interceptors which can be used to modify or transform requests or responses before they are handled by then or catch.
  • Supports automatic token refresh.

fetch

  • Supports cancelable requests and allows developers to abort requests at any point. 
  • Supports a wide range of HTTP methods beyond the standard GET and POST. 
  • Provides a Promise-based API, which simplifies the process of making asynchronous requests. 

request

  • Provides full access to all Node.js features, including streams, event emitters, and files.
  • Is highly configurable and can be used to set timeouts, retry requests, and more.
  • Has built-in support for gzip and deflate encoding.

superagent

  • Allows easy setting headers, cookies, and other request parameters. 
  • Is highly configurable and supports many features that help developers quickly and easily request HTTP.  
  • Supports multipart encoding, allowing developers to send binary data easily. 

nock

  • Allows users to easily create custom request matchers, allowing users to create more powerful and precise requests. 
  • Users can record and replay their requests, making debugging and testing easy.  
  • Provides a sandbox for users to test their requests in isolation without making live calls or affecting the external environment. 

node-fetch

  • Supports global agent pooling and request pooling.
  • Allows developers to use the same API for both server and client-side requests.
  • Is designed to be lightweight and fast, making it ideal for applications that require rapid response times.

r2

  • Provides support for the latest security protocols, such as TLS 1.3.
  • Support for advanced HTTP methodologies such as HTTP/2 and WebSocket.
  • Allows for the implementation of custom middleware.

needle

  • Has built-in support for parsing JSON and can be used to create custom parsers for other formats.  
  • Is well-documented and provides a comprehensive set of APIs for making HTTP requests.  
  • Is optimized for low latency and high throughput, making it suitable for various applications. 

unirest-nodejs

  • Has built-in support for automatically following redirects.
  • Allows to send both synchronous and asynchronous requests.
  • Has built-in support for automatically compressing and decompressing requests and responses.

hyperquest

  • Has built-in support for caching and retrying requests, making it easy to build resilient applications.  
  • Supports server-side data serialization and deserialization, allowing developers to transform data between client and server quickly.  
  • Is built on a minimalistic core, making it suitable for applications that need a small footprint. 

Here are some of the famous Python FastAPI Utility Libraries. The use cases of these libraries include Developing an API Backend, Automating System Tasks, Real-Time Data Processing, and Building Machine Learning Applications. 


Python FastAPI Utility Libraries are a set of libraries designed to make it easier to work with the Python FastAPI web framework. These libraries provide tools to help developers create and maintain their applications quickly and easily. They provide features such as automated documentation, robust request routing, validation, testing, logging, and more. 


Let us look at some of these famous libraries. 

loguru

  • Provides a rich set of features for tracking and debugging errors.
  • Supports integration with other Python libraries and frameworks.
  • Provides a number of convenience functions for quickly setting up logging with default configurations.

pydantic

  • Allows complex validations of data types, such as nested objects and lists.
  • Allows easy customization of data types and validation rules.
  • Provides an effortless way to convert data types between different formats, such as JSON, YAML, and XML. 

jinja

  • Provides a simple syntax for creating variables, loops, if-then-else statements, and more.
  • Provides developers with the ability to extend the language with custom filters, macros and functions.
  • Provides support for template inheritance.

starlette

  • Supports both ASGI and WSGI applications and works with any ASGI server.
  • Supports extensive type-hinting, making it easy to write strongly-typed code.
  • Provides a built-in debugger, making it easy to debug an application in real-time.

graphene

  • Provides an easy way to define custom resolvers, allowing one to build powerful custom GraphQL APIs.
  • Integrates seamlessly with existing Python libraries such as SQLAlchemy, Django ORM, and MongoEngine.
  • Provides built-in support for asynchronous and real-time GraphQL.

uvicorn

  • Supports a wide range of Python versions, from Python 3.6 and up.
  • Provides a convenient command-line interface for developers.
  • Offers a wide range of configuration options to customize the server to the application's needs. 

pyjwt

  • Supports cryptographic operations such as digital signatures and encryption.
  • Provides APIs for creating and validating JWT tokens.
  • Designed to be extensible, allowing developers to add their own custom algorithms or other features.

tortoise-orm

  • Allows users to define their models mapped to the underlying database.  
  • Provides powerful support for transactions.  
  • Provides a powerful query builder, making creating and executing complex queries easier. 

authlib

  • Provides an API for custom authentication flows.
  • Supports a variety of authentication methods that can be used to protect APIs.
  • Provides comprehensive documentation and a rich set of developer tools.

fastapi-utils

  • Provides built-in schema validation, so developers can easily ensure their API requests are valid.  
  • Allows developers to map requests and responses between their API and other services easily.  
  • Provides an effortless way for developers to create mock data for testing and development. 

Here are the best open-source JavaScript/jQuery libraries for making AJAX calls. You can use these to create fast and responsive web applications by allowing data to be exchanged with a server in the background without requiring a page reload.


JavaScript and jQuery are the most used programming languages for creating dynamic web pages. One of the most powerful features of these languages is the ability to make AJAX calls. AJAX stands for Asynchronous JavaScript and XML. JavaScript and jQuery provide several libraries for making AJAX calls. These libraries provide different ways to handle data exchange with a server. They all follow the same basic principle of sending a request to the server and processing the response. Also, you can use the built-in JavaScript objects that provide an easy way to send and receive data from a server—allowing developers to create asynchronous requests and handle the response with event listeners. These libraries simplify the process of making AJAX calls by providing various options for handling the request and response. They also provide a convenient way to handle errors and timeouts.


Moreover, newer JavaScript libraries provide a more modern and streamlined way of making AJAX calls. They use a simpler syntax for making requests and handling responses. You also get support for JSON and other data formats.


We have handpicked the top and trending open-source JavaScript/jQuery libraries for making AJAX calls in your next application development project.

jQuery:

  • Used in Plugin, jQuery applications, etc.
  • Provides a simple and intuitive way to make AJAX calls.
  • Functionality includes support for JSON, XML, and HTML data formats, as well as error handling and callbacks.

Axios:

  • Used to make HTTP requests. 
  • It’s a modern and lightweight JavaScript library.
  • Features easy-to-use syntax and support both client-side and server-side code.

Fetch:

  • Used in Programming Style, Reactive Programming, Symfony applications, etc.
  • Offers an easy way to make AJAX calls.
  • Provides a simpler syntax than traditional AJAX libraries and supports promises for handling asynchronous requests.

Superagent:

  • Used in Server, Runtime Environment applications, etc.
  • It’s a lightweight, flexible JavaScript library that provides an easy way to make AJAX calls. 
  • Supports a wide range of data formats and provides features such as timeouts and error handling.

Request:

  • Used in Web Services, REST applications, etc.
  • It’s a simplified HTTP request client for making HTTP requests.
  • Supports a wide range of data formats and provides features such as streaming and multipart requests.

Axios-retry:

  • Used in User Interface, State Container, Vue, Axios applications, etc.
  • It’s an extension of the Axios library that provides automatic retry functionality for failed requests.
  • Supports configurable retry intervals and can be used with browser and server-side code.

Bluebird:

  • Used for working with promises in Programming Style, Reactive Programming, Nodejs applications, etc.
  • Provides advanced features such as error handling, cancellation, and concurrency control.
  • It’s a fully featured promise library making it an ideal choice for complex AJAX requests.

jQuery.ajaxQueue:

  • Used to manage multiple AJAX requests in a queue.
  • Allows to specify the order in which requests are processed.
  • It’s a jQuery plugin that provides options for handling errors and timeouts.

Axios-mock-adapter:

  • Used for unit testing and debugging.
  • Provides a way to mock HTTP requests for testing purposes.
  • It’s an extension of the Axios library, allowing developers to simulate responses from a server without making a request.

Trending Discussions on Programming Style

Use for loop or multiple prints?

Why doesn't the rangeCheck method in the java.util.ArrayList class check for negative index?

Are java streams able to lazilly reduce from map/filter conditions?

Are any{}, all{}, and none{} lazy operations in Kotlin?

Use map and zip to be more func style in 2 for loops

malloc a "member" of struct v.s. whole struct when struct is quite simple

Difference between Running time and Execution time in algorithm?

Lifetime of get method in postgres Rust

Is there a way to implement mapcar in Common Lisp using only applicative programming and avoiding recursion or iteration as programming styles?

Create TKinter label using class method

QUESTION

Use for loop or multiple prints?

Asked 2022-Mar-01 at 21:31

What programming style should I use?

1...
2print(1)
3print(2)
4

or

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7

The output is the same 1 and on the next line 2, but which version should I use as a Python programmer?

I mean the first version is redundant or not?

ANSWER

Answered 2022-Mar-01 at 21:31

It depends.

There is an old rule "three or more, use for". (source)

On the other hand, sometimes unrolling a loop can offer a speed-up. (But that's generally more true in C or assembly.)

You should do what makes your program more clear.

For example, in the code below, I wrote out the calculations for the ABD matrix of a fiber reinforced composite laminate, because making nested loops would make it more complex in this case;

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7    for la, z2, z3 in zip(layers, lz2, lz3):
8        # first row
9        ABD[0][0] += la.Q̅11 * la.thickness  # Hyer:1998, p. 290
10        ABD[0][1] += la.Q̅12 * la.thickness
11        ABD[0][2] += la.Q̅16 * la.thickness
12        ABD[0][3] += la.Q̅11 * z2
13        ABD[0][4] += la.Q̅12 * z2
14        ABD[0][5] += la.Q̅16 * z2
15        # second row
16        ABD[1][0] += la.Q̅12 * la.thickness
17        ABD[1][1] += la.Q̅22 * la.thickness
18        ABD[1][2] += la.Q̅26 * la.thickness
19        ABD[1][3] += la.Q̅12 * z2
20        ABD[1][4] += la.Q̅22 * z2
21        ABD[1][5] += la.Q̅26 * z2
22        # third row
23        ABD[2][0] += la.Q̅16 * la.thickness
24        ABD[2][1] += la.Q̅26 * la.thickness
25        ABD[2][2] += la.Q̅66 * la.thickness
26        ABD[2][3] += la.Q̅16 * z2
27        ABD[2][4] += la.Q̅26 * z2
28        ABD[2][5] += la.Q̅66 * z2
29        # fourth row
30        ABD[3][0] += la.Q̅11 * z2
31        ABD[3][1] += la.Q̅12 * z2
32        ABD[3][2] += la.Q̅16 * z2
33        ABD[3][3] += la.Q̅11 * z3
34        ABD[3][4] += la.Q̅12 * z3
35        ABD[3][5] += la.Q̅16 * z3
36        # fifth row
37        ABD[4][0] += la.Q̅12 * z2
38        ABD[4][1] += la.Q̅22 * z2
39        ABD[4][2] += la.Q̅26 * z2
40        ABD[4][3] += la.Q̅12 * z3
41        ABD[4][4] += la.Q̅22 * z3
42        ABD[4][5] += la.Q̅26 * z3
43        # sixth row
44        ABD[5][0] += la.Q̅16 * z2
45        ABD[5][1] += la.Q̅26 * z2
46        ABD[5][2] += la.Q̅66 * z2
47        ABD[5][3] += la.Q̅16 * z3
48        ABD[5][4] += la.Q̅26 * z3
49        ABD[5][5] += la.Q̅66 * z3
50        # Calculate unit thermal stress resultants.
51        # Hyer:1998, p. 445
52        Ntx += (la.Q̅11 * la.αx + la.Q̅12 * la.αy + la.Q̅16 * la.αxy) * la.thickness
53        Nty += (la.Q̅12 * la.αx + la.Q̅22 * la.αy + la.Q̅26 * la.αxy) * la.thickness
54        Ntxy += (la.Q̅16 * la.αx + la.Q̅26 * la.αy + la.Q̅66 * la.αxy) * la.thickness
55        # Calculate H matrix (derived from Barbero:2018, p. 181)
56        sb = 5 / 4 * (la.thickness - 4 * z3 / thickness ** 2)
57        H[0][0] += la.Q̅s44 * sb
58        H[0][1] += la.Q̅s45 * sb
59        H[1][0] += la.Q̅s45 * sb
60        H[1][1] += la.Q̅s55 * sb
61        # Calculate E3
62        c3 += la.thickness / la.E3
63

Source https://stackoverflow.com/questions/71314703

QUESTION

Why doesn't the rangeCheck method in the java.util.ArrayList class check for negative index?

Asked 2022-Feb-28 at 15:32
1/**
2 * Checks if the given index is in range.  If not, throws an appropriate
3 * runtime exception.  This method does *not* check if the index is
4 * negative: It is always used immediately prior to an array access,
5 * which throws an ArrayIndexOutOfBoundsException if index is negative.
6 */
7private void rangeCheck(int index) {
8    if (index >= size)
9        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
10}
11

From: jdk/ArrayList.java at jdk8-b120 · openjdk/jdk · GitHub

If we write the following code, both indexes are out of bounds, but the exception types are different.

1/**
2 * Checks if the given index is in range.  If not, throws an appropriate
3 * runtime exception.  This method does *not* check if the index is
4 * negative: It is always used immediately prior to an array access,
5 * which throws an ArrayIndexOutOfBoundsException if index is negative.
6 */
7private void rangeCheck(int index) {
8    if (index >= size)
9        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
10}
11import java.util.ArrayList;
12import java.util.List;
13
14public class Test {
15
16    public static void main(String[] args) {
17        List<String> list = new ArrayList<>();
18        list.add("");
19
20        try {
21            list.get(-1);
22        } catch (Exception e) {
23            e.printStackTrace();
24        }
25
26        try {
27            list.get(1);
28        } catch (Exception e) {
29            e.printStackTrace();
30        }
31    }
32
33}
34

The output is as follows:

1/**
2 * Checks if the given index is in range.  If not, throws an appropriate
3 * runtime exception.  This method does *not* check if the index is
4 * negative: It is always used immediately prior to an array access,
5 * which throws an ArrayIndexOutOfBoundsException if index is negative.
6 */
7private void rangeCheck(int index) {
8    if (index >= size)
9        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
10}
11import java.util.ArrayList;
12import java.util.List;
13
14public class Test {
15
16    public static void main(String[] args) {
17        List<String> list = new ArrayList<>();
18        list.add("");
19
20        try {
21            list.get(-1);
22        } catch (Exception e) {
23            e.printStackTrace();
24        }
25
26        try {
27            list.get(1);
28        } catch (Exception e) {
29            e.printStackTrace();
30        }
31    }
32
33}
34java.lang.ArrayIndexOutOfBoundsException: -1
35    at java.util.ArrayList.elementData(ArrayList.java:424)
36    at java.util.ArrayList.get(ArrayList.java:437)
37    at Test.main(Test.java:11)
38java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
39    at java.util.ArrayList.rangeCheck(ArrayList.java:659)
40    at java.util.ArrayList.get(ArrayList.java:435)
41    at Test.main(Test.java:17)
42

Related question:

  • Why does rangeCheckForAdd method in the java.util.ArrayList check for negative index?
  • Why doesn't java.util.Arrays.ArrayList do index out-of-bounds checking?

What confuses me is why their implementations are inconsistent? Are these methods written by different people with their own programming style? In other words, if out-of-bounds exceptions will eventually fire, then there is no need to check.

ANSWER

Answered 2022-Feb-28 at 14:23

It's a micro-optimization. For code clarity you might prefer the same exception for both, but when you're in a hot loop you'll want to avoid an unnecessary operation. ArrayList being an old class, the effect this has may have varied between times and JDK versions. If someone has enough interest they could benchmark it with 1.8 and newer JDKs to see how much of an optimization it is for get().

Since accessing a negative array index will fail anyway, there is no need to check for it. However the size of the ArrayList is not always the same as the size of its internal array, so it needs to be checked explicitly.

As to why rangeCheckForAdd does check for negative indexes, good question. Adding is slow anyway, so the micro-optimization wouldn't make much of a difference. Maybe they wanted consistent error messaging here.

Source https://stackoverflow.com/questions/71295603

QUESTION

Are java streams able to lazilly reduce from map/filter conditions?

Asked 2022-Jan-12 at 09:30

I am using a functional programming style to solve the Leetcode easy question, Count the Number of Consistent Strings. The premise of this question is simple: count the amount of values for which the predicate of "all values are in another set" holds.

I have two approaches, one which I am fairly certain behaves as I want it to, and the other which I am less sure about. Both produce the correct output, but ideally they would stop evaluating other elements after the output is in a final state.


1    public int countConsistentStrings(String allowed, String[] words) {
2        final Set<Character> set = allowed.chars()
3          .mapToObj(c -> (char)c)
4          .collect(Collectors.toCollection(HashSet::new));
5        return (int)Arrays.stream(words)
6          .filter(word ->
7                  word.chars()
8                  .allMatch(c -> set.contains((char)c))
9                 )
10          .count();
11    }
12

In this solution, to the best of my knowledge, the allMatch statement will terminate and evaluate to false at the first instance of c for which the predicate does not hold true, skipping the other values in that stream.


1    public int countConsistentStrings(String allowed, String[] words) {
2        final Set<Character> set = allowed.chars()
3          .mapToObj(c -> (char)c)
4          .collect(Collectors.toCollection(HashSet::new));
5        return (int)Arrays.stream(words)
6          .filter(word ->
7                  word.chars()
8                  .allMatch(c -> set.contains((char)c))
9                 )
10          .count();
11    }
12    public int countConsistentStrings(String allowed, String[] words) {
13        Set<Character> set = allowed.chars()
14          .mapToObj(c -> (char)c)
15          .collect(Collectors.toCollection(HashSet::new));
16        return (int)Arrays.stream(words)
17          .filter(word ->
18                  word.chars()
19                  .mapToObj(c -> set.contains((char)c))
20                  .reduce((a,b) -> a&&b)
21                  .orElse(false)
22                 )
23          .count();
24    }
25

In this solution, the same logic is used but instead of allMatch, I use map and then reduce. Logically, after a single false value comes from the map stage, reduce will always evaluate to false. I know Java streams are lazy, but I am unsure when they ''know'' just how lazy they can be. Will this be less efficient than using allMatch or will laziness ensure the same operation?


Lastly, in this code, we can see that the value for x will always be 0 as after filtering for only positive numbers, the sum of them will always be positive (assume no overflow) so taking the minimum of positive numbers and a hardcoded 0 will be 0. Will the stream be lazy enough to evaluate this to 0 always, or will it work to reduce every element after the filter anyways?

1    public int countConsistentStrings(String allowed, String[] words) {
2        final Set<Character> set = allowed.chars()
3          .mapToObj(c -> (char)c)
4          .collect(Collectors.toCollection(HashSet::new));
5        return (int)Arrays.stream(words)
6          .filter(word ->
7                  word.chars()
8                  .allMatch(c -> set.contains((char)c))
9                 )
10          .count();
11    }
12    public int countConsistentStrings(String allowed, String[] words) {
13        Set<Character> set = allowed.chars()
14          .mapToObj(c -> (char)c)
15          .collect(Collectors.toCollection(HashSet::new));
16        return (int)Arrays.stream(words)
17          .filter(word ->
18                  word.chars()
19                  .mapToObj(c -> set.contains((char)c))
20                  .reduce((a,b) -> a&&b)
21                  .orElse(false)
22                 )
23          .count();
24    }
25List<Integer> list = new ArrayList<>();
26...
27/*Some values added to list*/
28...
29int x = list.stream()
30        .filter(i -> i >= 0)
31        .reduce((a,b) -> Math.min(a+b, 0))
32        .orElse(0);
33

To summarize the above, how does one know when the Java stream will be lazy? There are lazy opportunities that I see in the code, but how can I guarantee that my code will be as lazy as possible?

ANSWER

Answered 2022-Jan-12 at 09:30

The actual term you’re asking for is short-circuiting

Further, some operations are deemed short-circuiting operations. An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time. Having a short-circuiting operation in the pipeline is a necessary, but not sufficient, condition for the processing of an infinite stream to terminate normally in finite time.

The term “lazy” only applies to intermediate operations and means that they only perform work when being requested by the terminal operation. This is always the case, so when you don’t chain a terminal operation, no intermediate operation will ever process any element.

Finding out whether a terminal operation is short-circuiting, is rather easy. Go to the Stream API documentation and check whether the particular terminal operation’s documentation contains the sentence

This is a short-circuiting terminal operation.

allMatch has it, reduce has not.

This does not mean that such optimizations based on logic or algebra are impossible. But the responsibility lies at the JVM’s optimizer which might do the same for loops. However, this requires inlining of all involved methods to be sure that this conditions always applies and there are no side effect which must be retained. This behavioral compatibility implies that even if the processing gets optimized away, a peek(System.out::println) would keep printing all elements as if they were processed. In practice, you should not expect such optimizations, as the Stream implementation code is too complex for the optimizer.

Source https://stackoverflow.com/questions/70673795

QUESTION

Are any{}, all{}, and none{} lazy operations in Kotlin?

Asked 2022-Jan-12 at 01:03

I am using a functional programming style to solve the Leetcode easy question, Count the Number of Consistent Strings. The premise of this question is simple: count the amount of values for which the predicate of "all values are in another set" holds.

I was able to do this pretty concisely like so:

1class Solution {
2    fun countConsistentStrings(allowed: String, words: Array<String>): Int {
3      val permitted = allowed.toSet()
4      return words.count{it.all{it in permitted}}
5    }
6}
7

I know that Java streams are lazy, but have read Kotlin is only lazy when asSequence is used and are otherwise eager.

For reductions to a boolean based on a predicate using any, none, or all, it makes the most sense to me that this should be done lazily (e.g. a single false in all should evaluate the whole expression to false and stop evaluating the predicate for other elements).

Are these operations implemented this way, or are they still done eagerly like other operations in Kotlin. If so, there a way to do them lazily?

ANSWER

Answered 2022-Jan-12 at 00:03

The docs don't explicitly say, but this is easy enough to test.

1class Solution {
2    fun countConsistentStrings(allowed: String, words: Array<String>): Int {
3      val permitted = allowed.toSet()
4      return words.count{it.all{it in permitted}}
5    }
6}
7class A : Iterable<String>, Iterator<String> {
8  public override fun iterator(): Iterator<String> {
9    return this
10  }
11  public override fun hasNext(): Boolean {
12    return true
13  }
14  public override fun next(): String {
15    return "test"
16  }
17}
18
19fun main(args: Array<String>) {
20  val a = A()
21  println(a.any { x -> x == "test" })
22  println(a.none { x -> x == "test" })
23  println(a.all { x -> x != "test" })
24}
25

Here, A is a silly iterable class that just produces "test" forever and never runs out. Then we use any, none, and all to check whether it produces "test" or not. It's an infinite iterable, so if any of these three functions wanted to try to exhaust it, the program would hang forever. But you can run this yourself, and you'll see a true and two false's. The program terminates. So each of those three functions stopped when it found, respectively, a match, a non-match, and a non-match.

Try it online!

Source https://stackoverflow.com/questions/70674745

QUESTION

Use map and zip to be more func style in 2 for loops

Asked 2021-Oct-19 at 03:58

I implemented the following code to calculate weighted avg with for loops, how can I be more func programming style and use map and zip?

1val aggAvg = (emb: Seq[Seq[Float]], weights: Seq[Float]) => {
2      val embSize = emb.head.size
3      val len = emb.size
4      (0 until embSize)
5        .map { i =>
6          (0 until len).map { j =>
7            emb(j)(i) * weights(j)
8          }.sum / weights.sum
9        }
10    }
11

Example: Given

1val aggAvg = (emb: Seq[Seq[Float]], weights: Seq[Float]) => {
2      val embSize = emb.head.size
3      val len = emb.size
4      (0 until embSize)
5        .map { i =>
6          (0 until len).map { j =>
7            emb(j)(i) * weights(j)
8          }.sum / weights.sum
9        }
10    }
11val emb: Seq[Seq[Float]] = Seq(Seq(1,2,3), Seq(4,5,6))
12val weights: Seq[Float] = Seq(2, 8)
13

the output would be Seq(3.4, 4.4, 5.4) because (1 * 2 + 4 * 8) / (2 + 8) = 3.4 and so on.

ANSWER

Answered 2021-Oct-19 at 00:00

Here is one way, although I'm not sure if it's the most elegant

1val aggAvg = (emb: Seq[Seq[Float]], weights: Seq[Float]) => {
2      val embSize = emb.head.size
3      val len = emb.size
4      (0 until embSize)
5        .map { i =>
6          (0 until len).map { j =>
7            emb(j)(i) * weights(j)
8          }.sum / weights.sum
9        }
10    }
11val emb: Seq[Seq[Float]] = Seq(Seq(1,2,3), Seq(4,5,6))
12val weights: Seq[Float] = Seq(2, 8)
13val aggAvg = (emb: Seq[Seq[Float]], weights: Seq[Float]) =>
14  emb.transpose.map((weights, _).zipped.map(_ * _).sum).map(_ / weights.sum)
15res0: Seq[Float] = List(3.4, 4.4, 5.4)
16

Source https://stackoverflow.com/questions/69623371

QUESTION

malloc a "member" of struct v.s. whole struct when struct is quite simple

Asked 2021-Sep-23 at 16:33

I have searched on this site the topics about malloc on structs. However, I have a slightly problem. Is that malloc on the element of a struct different from malloc on the whole struct, especially when that struct is quite simple, that is, only a member that is exactly what we all want to allocate? To be clear, see the code corresponding to student and student2 structs below.

1struct student {
2    int* majorScore;
3};
4
5struct student2 {
6    int majorScore[3];
7};
8
9
10int main()
11{
12    struct student john;
13    john.majorScore = (int*) malloc(sizeof(int) * 3);
14    john.majorScore[0] = 50;
15    john.majorScore[1] = 27;
16    john.majorScore[2] = 56;
17 
18    struct student2* amy= (struct student2*)malloc(sizeof(struct student2));
19    amy->majorScore[0] = 50;
20    amy->majorScore[1] = 27;
21    amy->majorScore[2] = 56;
22
23
24    return 0;
25}
26

Are they different in memory level? If yes, what is the difference? If no, which is perhaps better in terms of a good programming style?

ANSWER

Answered 2021-Sep-23 at 16:15

First, you dynamically allocate one struct, but not the other. So you're comparing apples to oranges.


Statically-allocated structs:

1struct student {
2    int* majorScore;
3};
4
5struct student2 {
6    int majorScore[3];
7};
8
9
10int main()
11{
12    struct student john;
13    john.majorScore = (int*) malloc(sizeof(int) * 3);
14    john.majorScore[0] = 50;
15    john.majorScore[1] = 27;
16    john.majorScore[2] = 56;
17 
18    struct student2* amy= (struct student2*)malloc(sizeof(struct student2));
19    amy->majorScore[0] = 50;
20    amy->majorScore[1] = 27;
21    amy->majorScore[2] = 56;
22
23
24    return 0;
25}
26struct student john;
27john.majorScore = malloc(sizeof(int) * 3);
28john.majorScore[0] = 50;
29john.majorScore[1] = 27;
30john.majorScore[2] = 56;
31
32struct student2 amy;
33amy.majorScore[0] = 50;
34amy.majorScore[1] = 27;
35amy.majorScore[2] = 56;
36
1struct student {
2    int* majorScore;
3};
4
5struct student2 {
6    int majorScore[3];
7};
8
9
10int main()
11{
12    struct student john;
13    john.majorScore = (int*) malloc(sizeof(int) * 3);
14    john.majorScore[0] = 50;
15    john.majorScore[1] = 27;
16    john.majorScore[2] = 56;
17 
18    struct student2* amy= (struct student2*)malloc(sizeof(struct student2));
19    amy->majorScore[0] = 50;
20    amy->majorScore[1] = 27;
21    amy->majorScore[2] = 56;
22
23
24    return 0;
25}
26struct student john;
27john.majorScore = malloc(sizeof(int) * 3);
28john.majorScore[0] = 50;
29john.majorScore[1] = 27;
30john.majorScore[2] = 56;
31
32struct student2 amy;
33amy.majorScore[0] = 50;
34amy.majorScore[1] = 27;
35amy.majorScore[2] = 56;
36struct student john
37+------------+----------+      +----------+
38| majorScore |         ------->|       50 |
39+------------+----------+      +----------+
40| [padding]  |          |      |       27 |
41+------------+----------+      +----------+
42                               |       56 |
43                               +----------+
44
45struct student2 amy
46+------------+----------+
47| majorScore |       50 |
48|            +----------+
49|            |       27 |
50|            +----------+
51|            |       56 |
52+------------+----------+
53| [padding]  |          |
54+------------+----------+
55

struct student uses more memory because it has an extra value (the pointer), and it has the overhead of two memory blocks instead of one.

struct student2 always has memory for exactly three scores, even if you need fewer. And it can't possibly accommodate more than 3.


Dynamically-allocated structs:

1struct student {
2    int* majorScore;
3};
4
5struct student2 {
6    int majorScore[3];
7};
8
9
10int main()
11{
12    struct student john;
13    john.majorScore = (int*) malloc(sizeof(int) * 3);
14    john.majorScore[0] = 50;
15    john.majorScore[1] = 27;
16    john.majorScore[2] = 56;
17 
18    struct student2* amy= (struct student2*)malloc(sizeof(struct student2));
19    amy->majorScore[0] = 50;
20    amy->majorScore[1] = 27;
21    amy->majorScore[2] = 56;
22
23
24    return 0;
25}
26struct student john;
27john.majorScore = malloc(sizeof(int) * 3);
28john.majorScore[0] = 50;
29john.majorScore[1] = 27;
30john.majorScore[2] = 56;
31
32struct student2 amy;
33amy.majorScore[0] = 50;
34amy.majorScore[1] = 27;
35amy.majorScore[2] = 56;
36struct student john
37+------------+----------+      +----------+
38| majorScore |         ------->|       50 |
39+------------+----------+      +----------+
40| [padding]  |          |      |       27 |
41+------------+----------+      +----------+
42                               |       56 |
43                               +----------+
44
45struct student2 amy
46+------------+----------+
47| majorScore |       50 |
48|            +----------+
49|            |       27 |
50|            +----------+
51|            |       56 |
52+------------+----------+
53| [padding]  |          |
54+------------+----------+
55struct student *john = malloc(sizeof(struct student));
56john->majorScore = malloc(sizeof(int) * 3);
57john->majorScore[0] = 50;
58john->majorScore[1] = 27;
59john->majorScore[2] = 56;
60
61struct student2 *amy = malloc(sizeof(struct student2));
62amy->majorScore[0] = 50;
63amy->majorScore[1] = 27;
64amy->majorScore[2] = 56;
65
1struct student {
2    int* majorScore;
3};
4
5struct student2 {
6    int majorScore[3];
7};
8
9
10int main()
11{
12    struct student john;
13    john.majorScore = (int*) malloc(sizeof(int) * 3);
14    john.majorScore[0] = 50;
15    john.majorScore[1] = 27;
16    john.majorScore[2] = 56;
17 
18    struct student2* amy= (struct student2*)malloc(sizeof(struct student2));
19    amy->majorScore[0] = 50;
20    amy->majorScore[1] = 27;
21    amy->majorScore[2] = 56;
22
23
24    return 0;
25}
26struct student john;
27john.majorScore = malloc(sizeof(int) * 3);
28john.majorScore[0] = 50;
29john.majorScore[1] = 27;
30john.majorScore[2] = 56;
31
32struct student2 amy;
33amy.majorScore[0] = 50;
34amy.majorScore[1] = 27;
35amy.majorScore[2] = 56;
36struct student john
37+------------+----------+      +----------+
38| majorScore |         ------->|       50 |
39+------------+----------+      +----------+
40| [padding]  |          |      |       27 |
41+------------+----------+      +----------+
42                               |       56 |
43                               +----------+
44
45struct student2 amy
46+------------+----------+
47| majorScore |       50 |
48|            +----------+
49|            |       27 |
50|            +----------+
51|            |       56 |
52+------------+----------+
53| [padding]  |          |
54+------------+----------+
55struct student *john = malloc(sizeof(struct student));
56john->majorScore = malloc(sizeof(int) * 3);
57john->majorScore[0] = 50;
58john->majorScore[1] = 27;
59john->majorScore[2] = 56;
60
61struct student2 *amy = malloc(sizeof(struct student2));
62amy->majorScore[0] = 50;
63amy->majorScore[1] = 27;
64amy->majorScore[2] = 56;
65struct student *john
66+----------+      +------------+----------+      +----------+
67|         ------->| majorScore |         ------->|       50 |
68+----------+      +------------+----------+      +----------+
69                  | [padding]  |          |      |       27 |
70                  +------------+----------+      +----------+
71                                                 |       56 |
72                                                 +----------+
73
74struct student2 *amy
75+----------+      +------------+----------+
76|         ------->| majorScore |       50 |
77+----------+      |            +----------+
78                  |            |       27 |
79                  |            +----------+
80                  |            |       56 |
81                  +------------+----------+
82                  | [padding]  |          |
83                  +------------+----------+
84

Same analysis as above.

Source https://stackoverflow.com/questions/69303212

QUESTION

Difference between Running time and Execution time in algorithm?

Asked 2021-Aug-08 at 08:01

I'm currently reading this book called CLRS 2.2 page 25. In which the author describes the Running time of an algorithm as

The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed.

Also the author uses the running time to analyze algorithms. Then I referred a book called Data Structures and Algorithms made easy by Narasimha Karumanchi. In which he describes the following.

1.7 Goal of the Analysis of Algorithms The goal of the analysis of algorithms is to compare algorithms (or solutions) mainly in terms of running time but also in terms of other factors (e.g., memory, developer effort, etc.)

1.9 How to Compare Algorithms: To compare algorithms, let us define a few objective measures:

Execution times? Not a good measure as execution times are specific to a particular computer.

Number of statements executed? Not a good measure, since the number of statements varies with the programming language as well as the style of the individual programmer.

Ideal solution? Let us assume that we express the running time of a given algorithm as a function of the input size n (i.e., f(n)) and compare these different functions corresponding to running times. This kind of comparison is independent of machine time, programming style, etc.

As you can see from CLRS the author describes the running time as the number of steps executed whereas in the second book the author says its not a good measure to use Number of step executed to analyze the algorithms. Also the running time depends on the computer (my assumption) but the author from the second book says that we cannot consider the Execution time to analyze algorithms as it totally depends on the computer.

I thought the execution time and the running time are same!

So,

  • What is the real meaning or definition of running time and execution time? Are they the same of different?
  • Does running time describe the number of steps executed or not?
  • Does running time depend on the computer or not?

thanks in advance.

ANSWER

Answered 2021-Aug-08 at 07:57

What is the real meaning or definition of running time and execution time? Are they the same of different?

The definition of "running time" in 'Introduction to Algorithms' by C,L,R,S [CLRS] is actually not a time, but a number of steps. This is not what you would intuitively use as a definition. Most would agree that "runnning" and "executing" are the same concept, and that "time" is expressed in a unit of time (like milliseconds). So while we would normally consider these two terms to have the same meaning, in CLRS they have deviated from that, and gave a different meaning to "running time".

Does running time describe the number of steps executed or not?

It does mean that in CLRS. But the definition that CLRS uses for "running time" is particular, and not the same as you might encounter in other resources.

CLRS assumes here that a primitive operation (i.e. a step) takes O(1) time. This is typically true for CPU instructions, which take up to a fixed maximum number of cycles (where each cycle represents a unit of time), but it may not be true in higher level languages. For instance, some languages have a sort instruction. Counting that as a single "step" would give useless results in an analysis.

Breaking down an algorithm into its O(1) steps does help to analyse the complexity of an algorithm. Counting the steps for different inputs may only give a hint about the complexity though. Ultimately, the complexity of an algorithm requires a (mathematical) proof, based on the loops and the known complexity of the steps used in an algorithm.

Does running time depend on the computer or not?

Certainly the execution time may differ. This is one of the reasons we want to by a new computer once in a while.

The number of steps may depend on the computer. If both support the same programming language, and you count steps in that language, then: yes. But if you would do the counting more thoroughly and would count the CPU instructions that are actually ran by the compiled program, then it might be different. For instance, a C compiler on one computer may generate different machine code than a different C compiler on another computer, and so the number of CPU instructions may be less on the one than the other, even though they result from the same C program code.

Practically however, this counting at CPU instruction level is not relevant for determining the complexity of an algorithm. We generally know the time complexity of each instruction in the higher level language, and that is what counts for determining the overall complexity of an algorithm.

Source https://stackoverflow.com/questions/68698460

QUESTION

Lifetime of get method in postgres Rust

Asked 2021-Jun-14 at 07:09

Some Background (feel free to skip):

I'm very new to Rust, I come from a Haskell background (just in case that gives you an idea of any misconceptions I might have).

I am trying to write a program which, given a bunch of inputs from a database, can create customisable reports. To do this I wanted to create a Field datatype which is composable in a sort of DSL style. In Haskell my intuition would be to make Field an instance of Functor and Applicative so that writing things like this would be possible:

1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28

Actual Question:

I know that it's quite awkward to implement Functor and Applicative traits in Rust so I just implemented the appropriate functions for Field rather than actually defining traits (this all compiled fine). Here's a very simplified implementation of Field in Rust, without any of the Functor or Applicative stuff.

1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28use std::result;
29use postgres::Row;
30use postgres::types::FromSql;
31
32type Env = Vec<String>;
33
34type FieldFunction<A> = Box<dyn Fn(&Env, &Row) -> Result<A, String>>;
35
36struct Field<A> {
37    field_parse: FieldFunction<A>
38}
39

I can easily create a function which simply gets the value from an input field and creates a report Field with it:

1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28use std::result;
29use postgres::Row;
30use postgres::types::FromSql;
31
32type Env = Vec<String>;
33
34type FieldFunction<A> = Box<dyn Fn(&Env, &Row) -> Result<A, String>>;
35
36struct Field<A> {
37    field_parse: FieldFunction<A>
38}
39fn field_good(input: u32) -> Field<String> {
40    let f = Box::new(move |_: &Env, row: &Row| {
41        Ok(row.get(input as usize))
42    });
43
44    Field { field_parse: f }
45}
46

But when I try to make this polymorphic rather than using String I get some really strange lifetime errors that I just don't understand:

1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28use std::result;
29use postgres::Row;
30use postgres::types::FromSql;
31
32type Env = Vec<String>;
33
34type FieldFunction<A> = Box<dyn Fn(&Env, &Row) -> Result<A, String>>;
35
36struct Field<A> {
37    field_parse: FieldFunction<A>
38}
39fn field_good(input: u32) -> Field<String> {
40    let f = Box::new(move |_: &Env, row: &Row| {
41        Ok(row.get(input as usize))
42    });
43
44    Field { field_parse: f }
45}
46fn field_bad<'a, A: FromSql<'a>>(input: u32) -> Field<A> {
47    let f = Box::new(move |_: &Env, row: &Row| {
48        Ok(row.get(input as usize))
49    });
50
51    Field { field_parse: f }
52}
53
1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28use std::result;
29use postgres::Row;
30use postgres::types::FromSql;
31
32type Env = Vec<String>;
33
34type FieldFunction<A> = Box<dyn Fn(&Env, &Row) -> Result<A, String>>;
35
36struct Field<A> {
37    field_parse: FieldFunction<A>
38}
39fn field_good(input: u32) -> Field<String> {
40    let f = Box::new(move |_: &Env, row: &Row| {
41        Ok(row.get(input as usize))
42    });
43
44    Field { field_parse: f }
45}
46fn field_bad<'a, A: FromSql<'a>>(input: u32) -> Field<A> {
47    let f = Box::new(move |_: &Env, row: &Row| {
48        Ok(row.get(input as usize))
49    });
50
51    Field { field_parse: f }
52}
53error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
54  --> src/test.rs:36:16
55   |
5636 |         Ok(row.get(input as usize))
57   |                ^^^
58   |
59note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 35:22...
60  --> src/test.rs:35:22
61   |
6235 |       let f = Box::new(move |_: &Env, row: &Row| {
63   |  ______________________^
6436 | |         Ok(row.get(input as usize))
6537 | |     });
66   | |_____^
67note: ...so that reference does not outlive borrowed content
68  --> src/test.rs:36:12
69   |
7036 |         Ok(row.get(input as usize))
71   |            ^^^
72note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 34:14...
73  --> src/test.rs:34:14
74   |
7534 | fn field_bad<'a, A: FromSql<'a>>(input: FieldId) -> Field<A> {
76   |              ^^
77note: ...so that the types are compatible
78  --> src/test.rs:36:16
79   |
8036 |         Ok(row.get(input as usize))
81   |                ^^^
82   = note: expected `FromSql<'_>`
83              found `FromSql<'a>`
84

Any help explaining what this error is actually getting at or how to potentially fix it would be much appreciated. I included the Haskell stuff so that my design intentions are clear, that way if the problem is that I'm using a programming style that doesn't really work in Rust, then that could be pointed out to me.

EDIT: Forgot to include a link to the docs for postgres::Row::get in case it's relevant. They can be found here.

ANSWER

Answered 2021-Jun-10 at 12:54

So I seem to have fixed it, although I'm still not sure I understand exactly what I've done...

1type Env = [String]
2type Row = [String]
3
4data Field a = Field
5    { fieldParse :: Env -> Row -> a }
6
7instance Functor Field where
8    fmap f a = Field $
9        \env row -> f $ fieldParse a env row
10
11instance Applicative Field where
12    pure = Field . const . const
13    fa <*> fb = Field $
14        \env row -> (fieldParse fa) env row
15            $ (fieldParse fb) env row
16
17oneField :: Field Int
18oneField = pure 1
19
20twoField :: Field Int
21twoField = fmap (*2) oneField
22
23tripleField :: Field (Int -> Int)
24tripleField = pure (*3)
25
26threeField :: Field Int
27threeField = tripleField <*> oneField
28use std::result;
29use postgres::Row;
30use postgres::types::FromSql;
31
32type Env = Vec<String>;
33
34type FieldFunction<A> = Box<dyn Fn(&Env, &Row) -> Result<A, String>>;
35
36struct Field<A> {
37    field_parse: FieldFunction<A>
38}
39fn field_good(input: u32) -> Field<String> {
40    let f = Box::new(move |_: &Env, row: &Row| {
41        Ok(row.get(input as usize))
42    });
43
44    Field { field_parse: f }
45}
46fn field_bad<'a, A: FromSql<'a>>(input: u32) -> Field<A> {
47    let f = Box::new(move |_: &Env, row: &Row| {
48        Ok(row.get(input as usize))
49    });
50
51    Field { field_parse: f }
52}
53error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
54  --> src/test.rs:36:16
55   |
5636 |         Ok(row.get(input as usize))
57   |                ^^^
58   |
59note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 35:22...
60  --> src/test.rs:35:22
61   |
6235 |       let f = Box::new(move |_: &Env, row: &Row| {
63   |  ______________________^
6436 | |         Ok(row.get(input as usize))
6537 | |     });
66   | |_____^
67note: ...so that reference does not outlive borrowed content
68  --> src/test.rs:36:12
69   |
7036 |         Ok(row.get(input as usize))
71   |            ^^^
72note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 34:14...
73  --> src/test.rs:34:14
74   |
7534 | fn field_bad<'a, A: FromSql<'a>>(input: FieldId) -> Field<A> {
76   |              ^^
77note: ...so that the types are compatible
78  --> src/test.rs:36:16
79   |
8036 |         Ok(row.get(input as usize))
81   |                ^^^
82   = note: expected `FromSql<'_>`
83              found `FromSql<'a>`
84type FieldFunction<'a, A> = Box<dyn Fn(&Env, &'a Row) -> Result<A, String>>;
85
86struct Field<'a, A> {
87    field_parse: FieldFunction<'a, A>
88}
89
90fn field_bad<'a, A: FromSql<'a>>(input: u32) -> Field<'a, A> {
91    let f = Box::new(move |_: &Env, row: &'a Row| {
92        Ok(row.get(input as usize))
93    });
94
95    Field { field_parse: f }
96}
97

I also swear I tried this several times before but there we are...

Source https://stackoverflow.com/questions/67920269

QUESTION

Is there a way to implement mapcar in Common Lisp using only applicative programming and avoiding recursion or iteration as programming styles?

Asked 2021-May-25 at 10:22

I am trying to learn Common Lisp with the book Common Lisp: A gentle introduction to Symbolic Computation. In addition, I am using SBCL, Emacs and Slime.

In chapter 7, the author suggests there are three styles of programming the book will cover: recursion, iteration and applicative programming.

I am interested on the last one. This style is famous for the applicative operator funcall which is the primitive responsible for other applicative operators such as mapcar.

Thus, with an educational purpose, I decided to implement my own version of mapcar using funcall:

1(defun my-mapcar (fn xs)
2  (if (null xs)
3      nil
4      (cons (funcall fn (car xs))
5            (my-mapcar fn (cdr xs)))))
6

As you might see, I used recursion as a programming style to build an iconic applicative programming function.

It seems to work:

1(defun my-mapcar (fn xs)
2  (if (null xs)
3      nil
4      (cons (funcall fn (car xs))
5            (my-mapcar fn (cdr xs)))))
6CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
7(2 3 4 5)
8
9CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list ))
10NIL
11
12;; comparing the results with the official one
13
14CL-USER> (mapcar (lambda (n) (+ n 1)) (list ))
15NIL
16
17CL-USER> (mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
18(2 3 4 5)
19

Is there a way to implement mapcar without using recursion or iteration? Using only applicative programming as a style?

Thanks.

Obs.: I tried to see how it was implemented. But it was not possible

1(defun my-mapcar (fn xs)
2  (if (null xs)
3      nil
4      (cons (funcall fn (car xs))
5            (my-mapcar fn (cdr xs)))))
6CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
7(2 3 4 5)
8
9CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list ))
10NIL
11
12;; comparing the results with the official one
13
14CL-USER> (mapcar (lambda (n) (+ n 1)) (list ))
15NIL
16
17CL-USER> (mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
18(2 3 4 5)
19CL-USER> (function-lambda-expression #'mapcar)
20NIL
21T
22MAPCAR
23

I also used Emacs M-. to look for the documentation. However, the points below did not help me. I used this to find the files below:

1(defun my-mapcar (fn xs)
2  (if (null xs)
3      nil
4      (cons (funcall fn (car xs))
5            (my-mapcar fn (cdr xs)))))
6CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
7(2 3 4 5)
8
9CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list ))
10NIL
11
12;; comparing the results with the official one
13
14CL-USER> (mapcar (lambda (n) (+ n 1)) (list ))
15NIL
16
17CL-USER> (mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
18(2 3 4 5)
19CL-USER> (function-lambda-expression #'mapcar)
20NIL
21T
22MAPCAR
23/usr/share/sbcl-source/src/code/list.lisp
24  (DEFUN MAPCAR)
25/usr/share/sbcl-source/src/compiler/seqtran.lisp
26  (:DEFINE-SOURCE-TRANSFORM MAPCAR)
27/usr/share/sbcl-source/src/compiler/fndb.lisp
28  (DECLAIM MAPCAR SB-C:DEFKNOWN)
29

ANSWER

Answered 2021-May-21 at 17:36

mapcar is by itself a primitive applicative operator (pag. 220 of Common Lisp: A gentle introduction to Symbolic Computation). So, if you want to rewrite it in an applicative way, you should use some other primitive applicative operator, for instance map or map-into. For instance, with map-into:

1(defun my-mapcar (fn xs)
2  (if (null xs)
3      nil
4      (cons (funcall fn (car xs))
5            (my-mapcar fn (cdr xs)))))
6CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
7(2 3 4 5)
8
9CL-USER> (my-mapcar (lambda (n) (+ n 1)) (list ))
10NIL
11
12;; comparing the results with the official one
13
14CL-USER> (mapcar (lambda (n) (+ n 1)) (list ))
15NIL
16
17CL-USER> (mapcar (lambda (n) (+ n 1)) (list 1 2 3 4))
18(2 3 4 5)
19CL-USER> (function-lambda-expression #'mapcar)
20NIL
21T
22MAPCAR
23/usr/share/sbcl-source/src/code/list.lisp
24  (DEFUN MAPCAR)
25/usr/share/sbcl-source/src/compiler/seqtran.lisp
26  (:DEFINE-SOURCE-TRANSFORM MAPCAR)
27/usr/share/sbcl-source/src/compiler/fndb.lisp
28  (DECLAIM MAPCAR SB-C:DEFKNOWN)
29CL-USER> (defun my-mapcar (fn list &rest lists)
30           (apply #'map-into (make-list (length list)) fn list lists))
31MY-MAPCAR
32CL-USER> (my-mapcar #'1+ '(1 2 3))
33(2 3 4)
34CL-USER> (my-mapcar #'+ '(1 2 3) '(10 20 30) '(100 200 300))
35(111 222 333)
36

Source https://stackoverflow.com/questions/67640501

QUESTION

Create TKinter label using class method

Asked 2021-May-07 at 09:22

I am trying use object oriented programming style to write the code for a Tkinter app. I want to use a class method to place labels(or other widgets) to the GUI. The code I wrote is adding a character which I don't expect to the GUI. How can I write the initial add_label method so that it does not add the unwanted character. Below is my code and a screenshot. I am new to OOP, so i might be missing something.

1from tkinter import *
2class App:
3    def __init__(self, parent):
4        self.widgets(root)
5        self.add_label(root)
6
7    def widgets(self, app):
8        self.title = Label(app, text= 'LABEL UP').pack()
9        self.btn = Button(app, text = 'BUTTON').pack()
10    def add_label(self, text):
11        Label(text= text).pack()
12
13root = Tk()
14App(root)
15App.add_label(root, 'LABEL_1')
16App.add_label(root,'LABEL_2')
17root.mainloop()
18

enter image description here I am new to OOP and stil trying to figure out how i can benefit from code reuse in this case. My app has several widgets and functions

ANSWER

Answered 2021-May-07 at 09:22

What do you expect self.add_label(root) to do? According to your method definition, it takes text as argument, so when you say self.add_label(root), you are passing root as text. And what is root? It is '.', so remove it and it'll be gone.

Though a proper way to do this will be to pass a parent argument to the method and use that while widget creation:

And the important part is, your instantiating the class wrong. Keep a reference to it, rather than creating a lot of instances.

1from tkinter import *
2class App:
3    def __init__(self, parent):
4        self.widgets(root)
5        self.add_label(root)
6
7    def widgets(self, app):
8        self.title = Label(app, text= 'LABEL UP').pack()
9        self.btn = Button(app, text = 'BUTTON').pack()
10    def add_label(self, text):
11        Label(text= text).pack()
12
13root = Tk()
14App(root)
15App.add_label(root, 'LABEL_1')
16App.add_label(root,'LABEL_2')
17root.mainloop()
18from tkinter import *
19
20class App:
21    def __init__(self, parent):
22        self.widgets(root)
23
24    def widgets(self, app):
25        self.title = Label(app, text= 'LABEL UP').pack()
26        self.btn = Button(app, text = 'BUTTON').pack()
27    
28    def add_label(self, parent, text):
29        Label(parent,text= text).pack()
30
31root = Tk()
32
33app = App(root)
34app.add_label(root, 'LABEL_1')
35app.add_label(root,'LABEL_2')
36
37root.mainloop()
38

Try not to get confused with both the mistakes.

How would I write this class? I don't know the true purpose of this, but I think you can follow something like this:

1from tkinter import *
2class App:
3    def __init__(self, parent):
4        self.widgets(root)
5        self.add_label(root)
6
7    def widgets(self, app):
8        self.title = Label(app, text= 'LABEL UP').pack()
9        self.btn = Button(app, text = 'BUTTON').pack()
10    def add_label(self, text):
11        Label(text= text).pack()
12
13root = Tk()
14App(root)
15App.add_label(root, 'LABEL_1')
16App.add_label(root,'LABEL_2')
17root.mainloop()
18from tkinter import *
19
20class App:
21    def __init__(self, parent):
22        self.widgets(root)
23
24    def widgets(self, app):
25        self.title = Label(app, text= 'LABEL UP').pack()
26        self.btn = Button(app, text = 'BUTTON').pack()
27    
28    def add_label(self, parent, text):
29        Label(parent,text= text).pack()
30
31root = Tk()
32
33app = App(root)
34app.add_label(root, 'LABEL_1')
35app.add_label(root,'LABEL_2')
36
37root.mainloop()
38from tkinter import *
39
40class App:
41    def __init__(self, parent):
42        self.parent = parent
43        
44        self.title = Label(self.parent, text='LABEL UP')
45        self.title.pack()
46
47        self.entry = Entry(self.parent)
48        self.entry.pack()
49
50        self.btn = Button(self.parent, text='BUTTON')
51        # Compliance with PEP8
52        self.btn.config(command=lambda: self.add_label(self.entry.get())) 
53        self.btn.pack()
54
55    def add_label(self, text):
56        Label(self.parent, text=text).pack()
57
58    def start(self):
59        self.parent.mainloop()
60
61root = Tk()
62
63app = App(root)
64app.start()
65

Source https://stackoverflow.com/questions/67431872

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Programming Style

Tutorials and Learning Resources are not available at this moment for Programming Style

Share this Page

share link

Get latest updates on Programming Style