Explore all Performance Management open source software, libraries, packages, source code, cloud functions and APIs.

Explore Related Topics

Popular New Releases in Performance Management

netdata

v1.34.1

sentry

22.4.0

skywalking

9.0.0

osquery

uptime-kuma

Popular Libraries in Performance Management

netdata

by netdata doticoncdoticon

star image 58912 doticonGPL-3.0

Real-time performance monitoring, done right! https://www.netdata.cloud

sentry

by getsentry doticonpythondoticon

star image 30691 doticonNOASSERTION

Sentry is cross-platform application monitoring, with a focus on error reporting.

skywalking

by apache doticonjavadoticon

star image 19150 doticonApache-2.0

APM, Application Performance Monitoring System

osquery

by osquery doticonc++doticon

star image 18789 doticonNOASSERTION

SQL powered operating system instrumentation, monitoring, and analytics.

cat

by dianping doticonjavadoticon

star image 16597 doticonApache-2.0

CAT 作为服务端项目基础组件,提供了 Java, C/C++, Node.js, Python, Go 等多语言客户端,已经在美团点评的基础架构中间件框架(MVC框架,RPC框架,数据库框架,缓存框架等,消息队列,配置系统等)深度集成,为美团点评各业务线提供系统丰富的性能指标、健康状况、实时告警等。

uptime-kuma

by louislam doticonjavascriptdoticon

star image 16089 doticonMIT

A fancy self-hosted monitoring tool

goreplay

by buger doticongodoticon

star image 15413 doticonNOASSERTION

GoReplay is an open-source tool for capturing and replaying live HTTP traffic into a test environment in order to continuously test your system with real data. It can be used to increase confidence in code deployments, configuration changes and infrastructure changes.

zipkin

by openzipkin doticonjavadoticon

star image 15220 doticonApache-2.0

Zipkin is a distributed tracing system

goaccess

by allinurl doticoncdoticon

star image 14533 doticonMIT

GoAccess is a real-time web log analyzer and interactive viewer that runs in a terminal in *nix systems or through your browser.

Trending New libraries in Performance Management

uptime-kuma

by louislam doticonjavascriptdoticon

star image 16089 doticonMIT

A fancy self-hosted monitoring tool

howtheysre

by upgundecha doticonjavascriptdoticon

star image 6632 doticonCC0-1.0

A curated collection of publicly available resources on how technology and tech-savvy organizations around the world practice Site Reliability Engineering (SRE)

signoz

by SigNoz doticontypescriptdoticon

star image 6127 doticonMIT

SigNoz is an open-source APM. It helps developers monitor their applications & troubleshoot problems, an open-source alternative to DataDog, NewRelic, etc. 🔥 🖥. 👉 Open source Application Performance Monitoring (APM) & Observability tool

pyroscope

by pyroscope-io doticongodoticon

star image 5738 doticonApache-2.0

Continuous Profiling Platform. Debug performance issues down to a single line of code

nightingale

by didi doticongodoticon

star image 4433 doticonApache-2.0

An enterprise-level cloud-native monitoring system, which can be used as drop-in replacement of Prometheus for alerting and management.

SmsForwarder

by pppscn doticonjavadoticon

star image 3181 doticonBSD-2-Clause

短信转发器——监控Android手机短信、来电、APP通知,并根据指定规则转发到其他手机:钉钉机器人、企业微信群机器人、飞书机器人、企业微信应用消息、邮箱、bark、webhook、Telegram机器人、Server酱、PushPlus、手机短信等。PS.这个APK主要是学习与自用,如有BUG请提ISSUE,同时欢迎大家提PR指正

go-profiler-notes

by DataDog doticonjupyter notebookdoticon

star image 2336 doticonCC-BY-SA-4.0

felixge's notes on the various go profiling methods that are available.

fgprof

by felixge doticongodoticon

star image 1814 doticonMIT

🚀 fgprof is a sampling Go profiler that allows you to analyze On-CPU as well as Off-CPU (e.g. I/O) time together.

statsviz

by arl doticongodoticon

star image 1766 doticonMIT

:rocket: Instant live visualization of your Go application runtime statistics (GC, MemStats, etc.) in the browser

Top Authors in Performance Management

1

newrelic

67 Libraries

star icon2876

2

sensu-plugins

60 Libraries

star icon631

3

getsentry

49 Libraries

star icon45796

4

sensu

34 Libraries

star icon5453

5

Appdynamics

32 Libraries

star icon157

6

Icinga

26 Libraries

star icon3131

7

microsoft

25 Libraries

star icon2626

8

xfce-mirror

21 Libraries

star icon204

9

DataDog

16 Libraries

star icon8206

10

m4ce

14 Libraries

star icon78

1

67 Libraries

star icon2876

2

60 Libraries

star icon631

3

49 Libraries

star icon45796

4

34 Libraries

star icon5453

5

32 Libraries

star icon157

6

26 Libraries

star icon3131

7

25 Libraries

star icon2626

8

21 Libraries

star icon204

9

16 Libraries

star icon8206

10

14 Libraries

star icon78

Trending Kits in Performance Management

Here are some famous Java Monitoring Libraries. Some Java Monitoring Libraries' use cases include Performance Monitoring, Memory Usage Monitoring, Error Reporting, Metrics Collection, and Logging.


Java monitoring Libraries offers deep application performance monitoring with byte-code instrumentation for Java applications in development, QA, and production environments. Ensure that your end users have a satisfactory experience by simulating their journey through the applications.


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

pinpoint 

  • Real-time monitoring of your application. 
  • Get transaction visibility at the code level. 
  • Without altering a single line of code, install APM Agents. 

javamelody

  • Measure and calculate statistics on real operation of an application depending on the usage of the application by users.
  • Give facts about the average response times and number of executions.
  • Verify the real improvement after optimizations.

zabbix 

  • Enables you to collect metrics. 
  • Offers its users a variety of clever, adjustable threshold defining choices. 
  • Functions and operations that are supported for the statistical analysis of historical data. 

visualvm

  • Monitors and troubleshoots applications running on Java 1.4+.
  • Perfectly fits all the requirements of application developers, system administrators, and end users.
  • Provides detailed information about Java applications while they are running on the Java Virtual Machine.

log4j 

  • Supports multiple output appenders per logger. 
  • Not limited to a set of predetermined facilities. 
  • Seed-optimized and thread-safe. 

JHiccup

  • Allows developers and system operators to easily create and analyze response time profiles.
  • Unique in looking at underlying platform.
  • Helps to avoid common pitfall in application performance characterization.

jamonapi

  • Rich feature set to support encapsulation, parameterization, and reuse of presentation logic.
  • Declare the arguments required for rendering.
  • Allows passing arbitrary objects between templates.

relics 

  • Being able to monitor measurements and performance under pressure. 
  • Enhance output and identify bottlenecks. 
  • The capacity to probe deeply and evaluate the code stack. 

Docker is a platform for developing, shipping and running applications in containers. When you use Docker, you use different tools and libraries to make, control, and use containers.  


These libraries help with managing and developing containers. The libraries have tools for Docker. They have modular architecture and real-time monitoring. They also include workflow automation and curated cheat sheets.  


Here are the 14 essential Docker libraries:  

moby  

  • Moby has a modular architecture. Developers can pick and assemble only the components they need. It's for their specific use case.  
  • Moby offers essential Docker parts for users who want to make their own containers.  
  • Moby helps developers collaborate on container projects and experiment with containerization technologies.  

traefik   

  • Traefik can find and set up routes to backend services as they change in size.  
  • The system can handle different types of traffic like HTTP, TCP, and UDP.  
  • Traefik works well with container orchestration platforms such as Docker Swarm and Kubernetes. This makes it easier to handle ingress and load balancing for containerized applications.  

gogs  

  • Gogs is a lightweight Git service. It is self-hosted and has a simple, user-friendly interface for managing repositories.  
  • The software has tools for tracking issues and collaborating with others. It's an all-in-one solution for managing source code and issues.  
  • Users can customize Gogs to fit their needs and preferences.  

dive  

  • Dive is a tool for exploring container images layer by layer. It helps users understand and optimize image size and composition.  
  • Visual Diffs show changes between image layers, aiding troubleshooting and image optimization.  
  • Dive has an interactive CLI. It helps explore container images and analyze Docker images.  

docker_practice 

  • This library helps beginners learn Docker through hands-on exercises.  
  • The exercises begin with simple ideas, like making and using images. They become harder as the user gets better.  
  • The library has real-world examples of Docker, like deploying a web app and running CI.  

docker-development-youtube-series  

  • This library has video tutorials on Docker development. They cover Dockerfile basics, image management, and container orchestration.  
  • Each video tutorial has a code sample that users can use to follow.  
  • The library has a Discord server. Users can ask questions and get help there.  

docker-ce  

  • This is Docker distribution from Docker, Inc.  
  • The complete set of Docker features includes image building, container running, and orchestration.  
  • Docker tools and plugins widely support it.  

portainer  

  • This is a website where you can manage Docker containers and images.  
  • It has many features for managing Docker, like creating, managing, and monitoring containers.  
  • It is easy to use, even for users unfamiliar with Docker.  

Here are other Docker libraries with different programming languages.  

uptime-kuma  

  • Uptime-Kuma can check if services and protocols are available and performing well.  
  • Flexible Alerting helps users receive notifications when services encounter issues or become unavailable.  
  • Uptime-Kuma has a web interface that is easy to use. It lets you configure and see monitoring results. Many people can use it.  

awesome-cheatsheets  

  • The website Awesome-Cheatsheets has cheat sheets and references for programming languages and technologies.  
  • We encourage the community to contribute to keep the cheat sheets up to date and complete.  
  • Developers like the organized and user-friendly repository. It has helpful references and tips.  

n8n  

  • n8n is a tool for automating workflows. It lets users create and manage workflows visually. This makes it easy for both technical and non-technical users to use.  
  • Extensive Integration makes it easier to automate complex tasks with many pre-built integrations.  
  • Since n8n is open-source, you can host it on your server to manage your automation workflows and data.  

appwrite  

  • Appwrite is a platform for building web and mobile apps. It handles the backend. It provides many services, including authentication, storage, and databases.  
  • Appwrite is perfect for creating chat apps and document editors with real-time collaboration.  
  • It works on many platforms and programming languages so developers can use it for any app.

netdata  

  • Netdata is great for monitoring performance in real time. It has a dashboard to see system metrics and application performance.  
  • Users can customize their experience by adding plugins and integrations. These can monitor specific metrics and applications. This allows for greater flexibility and customization.  
  • Netdata has automatic anomaly detection. The system tells users when strange things happen and helps them find problems early.  

devops-exercises 

  • DevOps-Exercises offers exercises and resources to help people and teams practice DevOps principles.  
  • Gain practical experience with DevOps tools and methods through hands-on exercises.  
  • DevOps-Exercises values teamwork and welcomes contributions, which makes it valuable for DevOps enthusiasts.

Trending Discussions on Performance Management

How to check next element in a for loop based off of a condition?

Cannot Find the Reported APM Logs on the Huawei Devices

Find element in selenium for LinkedIn Job Page

**ERROR** TypeError: Cannot read property 'replace' of undefined

How To Handle White Space in Forloop from Dynamic Command to Create Directories

How to Account for White Space and Append Strings in Double Quotes in Bash Script

"statistics unavailable at this time" when trying to print String Pool information?

How to combine array of data and make is more easy to use

How to use update method in viewsets in Django Rest Framework and perform some task(like sending mail) along with it?

Is using 'ref' or 'out' common practice in C#

QUESTION

How to check next element in a for loop based off of a condition?

Asked 2022-Feb-22 at 05:13

So I am currently stuck on trying to iterate over my for loop while checking the elements based off of my condition:

Let's say I have this dictionary:

1testing_dic = {
2    "1800": [
3        "Human Capital",
4        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
5        "4",
6        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
7        "Health and Safety",
8        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
9        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
10        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
11        "Talent Management",
12        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
13        "Diversity and Inclusion ",
14        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
15        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
16        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
17        "5",
18        "Compensation and Benefits",
19        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
20    ]
21}
22

and I want to get the output below because I am trying to extract the Diversity and Inclusion Section for this particular corporation:

1testing_dic = {
2    "1800": [
3        "Human Capital",
4        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
5        "4",
6        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
7        "Health and Safety",
8        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
9        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
10        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
11        "Talent Management",
12        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
13        "Diversity and Inclusion ",
14        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
15        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
16        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
17        "5",
18        "Compensation and Benefits",
19        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
20    ]
21}
22        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
23        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
24        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
25        "5"
26

Code: Here is the current code I have. I do know that the logic is wrong, but I am unsure on how to proceed on getting in this case the element "Compensation and Benefits" and making the code output anything between the element "Diversity and Inclusion" to the "Compensation and Benefits".

1testing_dic = {
2    "1800": [
3        "Human Capital",
4        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
5        "4",
6        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
7        "Health and Safety",
8        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
9        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
10        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
11        "Talent Management",
12        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
13        "Diversity and Inclusion ",
14        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
15        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
16        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
17        "5",
18        "Compensation and Benefits",
19        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
20    ]
21}
22        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
23        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
24        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
25        "5"
26allFilings2021_part1_di_keywords = ["Diversity and Inclusion "]
27allFilings2021_part1_di_next_section_keywords = ["Compensation and Benefits"]
28di_dic = {}
29for k,v in testing_dic.items():
30    n = 1
31    di_list = []
32    for i in range(len(v)):
33        if v[i] in allFilings2021_part1_di_keywords:
34            if v[i+n] in allFilings2021_part1_di_next_section_keywords:
35                di_list.append(v[i:i+n])
36                di_dic[k] = di_list
37                n += 1
38        else:
39            continue
40

Any help is greatly appreciated.

ANSWER

Answered 2022-Feb-22 at 05:01

You could use list.index to get the index of each element. Then you can use slicing to get anything between them:

1testing_dic = {
2    "1800": [
3        "Human Capital",
4        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
5        "4",
6        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
7        "Health and Safety",
8        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
9        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
10        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
11        "Talent Management",
12        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
13        "Diversity and Inclusion ",
14        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
15        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
16        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
17        "5",
18        "Compensation and Benefits",
19        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
20    ]
21}
22        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
23        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
24        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
25        "5"
26allFilings2021_part1_di_keywords = ["Diversity and Inclusion "]
27allFilings2021_part1_di_next_section_keywords = ["Compensation and Benefits"]
28di_dic = {}
29for k,v in testing_dic.items():
30    n = 1
31    di_list = []
32    for i in range(len(v)):
33        if v[i] in allFilings2021_part1_di_keywords:
34            if v[i+n] in allFilings2021_part1_di_next_section_keywords:
35                di_list.append(v[i:i+n])
36                di_dic[k] = di_list
37                n += 1
38        else:
39            continue
40def find_between(e1, e2, elements):
41    return elements[elements.index(e1) + 1:elements.index(e2)]
42

This would be roughly equivalent to the following code:

1testing_dic = {
2    "1800": [
3        "Human Capital",
4        "The sustainability of Abbott’s business depends on attracting, engaging and developing talented people with diverse backgrounds who share Abbott’s mission to help people live their healthiest possible lives. Abbott provides its employees opportunities to grow and develop their careers, market competitive compensation and benefit programs, and the satisfaction of being part of a global company dedicated to improving health in more than 160 countries.",
5        "4",
6        "As of December 31, 2020, Abbott employed approximately 109,000 people, 70% of whom were employed outside of the U.S. Women represented 47% of Abbott’s U.S. workforce, 45% of its global workforce, and 39% of its managers.",
7        "Health and Safety",
8        "The health, safety and wellness of its employees is an Abbott priority embedded at every level of its business.  Abbott’s integrated Environmental, Health and Safety organization governs health, safety and wellness at Abbott’s facilities. Abbott also maintains global policies and standards for managing employee health and safety.",
9        "Abbott takes a holistic approach to employee well-being. Abbott’s global wellness programs are designed to meet the unique needs of employees across businesses and geographies and offer a wide range of programs, including supporting the mental, financial and physical health of employees and their families. For example, for over 20 years, Abbott has annually offered Exercise Across Abbott, which is a four-week physical wellness program that encourages employees to team up with colleagues and track how many minutes they exercise each day. Over 22,000 Abbott employees across 72 countries took part in 2020.",
10        "During the COVID-19 pandemic, Abbott has taken aggressive steps to limit exposure and enhance the safety of facilities for its employees, including implementing mandatory temperature screening and social distancing, providing and requiring the use of personal protective equipment, and at most U.S. facilities, onsite COVID-19 testing. ",
11        "Talent Management",
12        "Abbott has an integrated global talent management process that is designed to identify and assess talent across the organization and provide equal and consistent opportunities for employees to develop their skills. All levels of employees participate in Abbott’s annual performance management process to create development plans that support their particular career objectives, and Abbott provides a broad range of training, mentoring and other development opportunities to help its employees meet these objectives. The board of directors conducts an annual Talent Management Review, focusing on development of talent, diversity, and succession planning for critical positions. Similar reviews take place at every level of Abbott to develop talent and diversity across the organization. ",
13        "Diversity and Inclusion ",
14        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
15        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
16        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
17        "5",
18        "Compensation and Benefits",
19        "Abbott is committed to building, retaining, and motivating a diverse talent pipeline that can meet the current and future needs of its businesses. To that end, Abbott provides market competitive compensation, healthcare benefits, pension and/or retirement savings plans, and several programs to facilitate employees building an ownership stake in Abbott, including a global long-term incentive program for employees generally beginning at the manager level. Abbott also has procedures and processes focused on providing employees equitable compensation, regardless of race or gender or other personal characteristics."
20    ]
21}
22        "Abbott is committed to developing a workplace that is inclusive for all. Abbott ties executive compensation to human capital management, including diversity outcomes, to sustain an inclusive culture and the fair and balanced treatment of Abbott’s employees. ",
23        "Abbott’s employee networks play an important role in building an inclusive culture across all Abbott operations.  A member of Abbott’s senior management serves as a sponsor for each of these networks, helping to align their objectives with Abbott’s business strategies. Abbott has ten such networks, which are: Advancing Professionals Network (supporting early career employees), Asian Leadership and Cultural Network, Black Business Network, Flex Network (employees with part-time and flexible schedules), LA VOICE Network (supporting Hispanic and Latino employees), People with Disabilities Network, PRIDE (supporting LGBTQ employees), Veterans Network, Women Leaders of Abbott, and Women in STEM.  ",
24        "Abbott offers professional development programs, which provide recent college graduates the opportunity to rotate through different areas of Abbott, often with the chance to work outside their home country. In 2020, 52% of the participants were women. Also, Abbott hosts hundreds of college students for paid internships. In 2020, 55% of the U.S. interns were women and 39% were minorities. Further, Abbott has operated a STEM internship program for high school students in the U.S. since 2012. The program’s objective is to increase the number of students pursuing STEM-related careers and contribute to a more diverse talent pipeline for Abbott. In 2020, 58% of the STEM interns were women and 71% were minorities. ",
25        "5"
26allFilings2021_part1_di_keywords = ["Diversity and Inclusion "]
27allFilings2021_part1_di_next_section_keywords = ["Compensation and Benefits"]
28di_dic = {}
29for k,v in testing_dic.items():
30    n = 1
31    di_list = []
32    for i in range(len(v)):
33        if v[i] in allFilings2021_part1_di_keywords:
34            if v[i+n] in allFilings2021_part1_di_next_section_keywords:
35                di_list.append(v[i:i+n])
36                di_dic[k] = di_list
37                n += 1
38        else:
39            continue
40def find_between(e1, e2, elements):
41    return elements[elements.index(e1) + 1:elements.index(e2)]
42def find_between(e1, e2, elements):
43    found = []
44    iterator = iter(elements)
45    for e in iterator:
46        if e == e1:
47            break
48    for e in iterator:
49        if e == e2:
50            break
51        found.append(e)
52    return found
53

The difference is that version 1 raises an exception when an element does not exist while version two just finds nothing if this is the case for e1 and everything after e1 if this is the case for e2.

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

QUESTION

Cannot Find the Reported APM Logs on the Huawei Devices

Asked 2021-Oct-23 at 09:50

When the Huawei APM SDK is integrated, there was no app performance data on the App performance management page. I wanted to locate the problem based on the Logcat logs on the device.

However, I'm not sure where these logs are kept or if I have to enable them in some way?

ANSWER

Answered 2021-Oct-23 at 09:50

I checked the APM documentation and found out how to access the logs:

Open the AndroidManifest.xml file of the app, and add the meta-data element to the application.

1<application>  
2    <meta-data 
3      android:name="apms_debug_log_enabled" 
4      android:value=" true" /> 
5</application>
6

After the APM debug log function is enabled, you can use the Logcat log filter function com.huawei.agc.apms or apms to view the logs.

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

QUESTION

Find element in selenium for LinkedIn Job Page

Asked 2021-Sep-08 at 20:09

I am currently trying to scrape jobs on Linkedin i have written this code

1job = browser.find_elements_by_tag_name("a")
2c = []
34for i in job:
5    c.append(i.text)
6print(c)
7print((len(c))) 
8

This does not return correct output, I would like to retrieve the job title of every posting, the output this is giving me is

1job = browser.find_elements_by_tag_name("a")
2c = []
34for i in job:
5    c.append(i.text)
6print(c)
7print((len(c))) 
8['LinkedIn', 'new feed updates notifications\nHome', 'My Network', 'Jobs', 'Messaging', '1\n1 new notification\nNotifications', '', '', 'Global Analytic Insights Consultant', 'Concentrix', '6 alumni work here', '', 'Data Intelligence Engineer', 'Arrow Electronics', '', 'Junior Data Modeler', 'Teradata', '1 alum works here', '', 'Research Analyst - BASES', 'NielsenIQ', '1 alum works here', '', 'Group Visualisation and Reporting Specialist', 'Coca-Cola Beverages South Africa (CCBSA)', '', 'Group External Commercial Data Specialist', 'Coca-Cola Beverages South Africa (CCBSA)', '', 'Junior/ Business Analyst', 'Arrow Electronics', '', 'Technical Consultant (O365)', 'Microsoft', '2 connections work here', '', 'Business Analyst', '', 'Support Engineer for Power BI', 'Microsoft', '2 connections work here', '', 'Insights and Analytics Specialist', 'Souq.com', '', 'Call Center Representative', 'Raya CX', '', 'Work from Home Opportunities | Flexible Hours', 'Appen', '', 'Early Careers Program', 'DXC Technology', '1 alum works here', '', 'Business Intelligence Executive', 'noon', '', 'Performance Management Analyst', 'talabat', '7 alumni work here', '', 'IT Business Analyst', 'ALEXBANK', '30 alumni work here', '', 'Study Abroad', 'Educatly', '', 'Work from Home Opportunities | Flexible Hours', 'Appen', '', 'Associate Managing Consultant, Advisors', 'Mastercard', '', 'Assistant', 'NielsenIQ', '', 'Program Support Associate', 'Souq.com', '', 'Junior/ Business Analyst', 'Arrow Electronics', '', 'Internet Analyst', 'Appen', '', 'Oracle Cloud SCM / ERP Junior Coordinator (12 months contract)', 'Oracle', '1 connection works here', 'Try Premium for free', 'About', 'Accessibility', 'Help Center', 'Ad Choices', 'Advertising', '', '', '', '', '']
9101
10

Here is a link for the page:

linkedin.com/jobs/search/?geoId=106155005&location=Egypt

ANSWER

Answered 2021-Sep-08 at 20:09

This is because you are using a wrong locator.
There are a lot of elements with a tag name a on that page, most of them are not what you are looking for.
This is why you getting this result.
UPD
This code should work:

1job = browser.find_elements_by_tag_name("a")
2c = []
34for i in job:
5    c.append(i.text)
6print(c)
7print((len(c))) 
8['LinkedIn', 'new feed updates notifications\nHome', 'My Network', 'Jobs', 'Messaging', '1\n1 new notification\nNotifications', '', '', 'Global Analytic Insights Consultant', 'Concentrix', '6 alumni work here', '', 'Data Intelligence Engineer', 'Arrow Electronics', '', 'Junior Data Modeler', 'Teradata', '1 alum works here', '', 'Research Analyst - BASES', 'NielsenIQ', '1 alum works here', '', 'Group Visualisation and Reporting Specialist', 'Coca-Cola Beverages South Africa (CCBSA)', '', 'Group External Commercial Data Specialist', 'Coca-Cola Beverages South Africa (CCBSA)', '', 'Junior/ Business Analyst', 'Arrow Electronics', '', 'Technical Consultant (O365)', 'Microsoft', '2 connections work here', '', 'Business Analyst', '', 'Support Engineer for Power BI', 'Microsoft', '2 connections work here', '', 'Insights and Analytics Specialist', 'Souq.com', '', 'Call Center Representative', 'Raya CX', '', 'Work from Home Opportunities | Flexible Hours', 'Appen', '', 'Early Careers Program', 'DXC Technology', '1 alum works here', '', 'Business Intelligence Executive', 'noon', '', 'Performance Management Analyst', 'talabat', '7 alumni work here', '', 'IT Business Analyst', 'ALEXBANK', '30 alumni work here', '', 'Study Abroad', 'Educatly', '', 'Work from Home Opportunities | Flexible Hours', 'Appen', '', 'Associate Managing Consultant, Advisors', 'Mastercard', '', 'Assistant', 'NielsenIQ', '', 'Program Support Associate', 'Souq.com', '', 'Junior/ Business Analyst', 'Arrow Electronics', '', 'Internet Analyst', 'Appen', '', 'Oracle Cloud SCM / ERP Junior Coordinator (12 months contract)', 'Oracle', '1 connection works here', 'Try Premium for free', 'About', 'Accessibility', 'Help Center', 'Ad Choices', 'Advertising', '', '', '', '', '']
9101
10job_titles = browser.find_elements_by_css_selector("a.job-card-list__title")
11c = []
1213for title in job_titles:
14    c.append(title.text)
15print(c)
16print((len(c))) 
17

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

QUESTION

**ERROR** TypeError: Cannot read property 'replace' of undefined

Asked 2021-Aug-15 at 11:40

I got two object has some white space didn't match.
when I'm using two replace function in the for..of it reports error.
if I use just one replace it works .
but object mismatch

my code is here :

1 const jsonText = await tableFlyoutPanel.json.textJsonContent();
2const rowContent = await alarmTable.rowContentWithHeaders(1, true);
3for (const header of Object.keys(jsonText)) {
4  const text = jsonText[header].replace(/s+/gm, '');
5  const content = rowContent[header].replace(/s+/gm, '');
6  expect(text).toStrictEqual(expect.stringContaining(content));
7}
8

it returns error in "Content" .
ERROR CODE: TypeError: Cannot read property 'replace' of undefined.

1 const jsonText = await tableFlyoutPanel.json.textJsonContent();
2const rowContent = await alarmTable.rowContentWithHeaders(1, true);
3for (const header of Object.keys(jsonText)) {
4  const text = jsonText[header].replace(/s+/gm, '');
5  const content = rowContent[header].replace(/s+/gm, '');
6  expect(text).toStrictEqual(expect.stringContaining(content));
7}
8   console.log(rowContent);.
9 {
10  Time: '2021-07-10 03:42:01.000 (1 month ago)',
11  'Node name': 'sapc17',
12  'Node type': '-',
13  Severity: 'Major',
14  'Alarm name': 'Performance Management Threshold Crossed or Reached',
15  'Faulty resource': 'ManagedElement=1,SystemFunctions=1,Pm=1,PmJob=memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit=PL-3',
16  Description: 'Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3'
17}
18
19
20
21
22
23       console.log(jsonText);.
24

{ 'Node name': 'sapc17', 'Alarm name': 'Performance Management Threshold Crossed or Reached', Severity: 'Major', 'Faulty resource': 'ManagedElement=1,SystemFunctions=1,Pm=1,PmJob =memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit =PL-3', 'Fault id': '628', Description: 'Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit ,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3', Time: '2021-07-10 03:42:01.000 (1 month ago)', 'Event type': 'QUALITYOFSERVICEALARM', 'Node type': '-', 'Probable cause': '-', Code: '-', Action: '-' }

ANSWER

Answered 2021-Aug-14 at 16:28

This means that rowContent[header] is undefined.

Inspect rowContent - e.g. by placing a console.log(rowContent); after const rowContent = .... this should give you some clues and maybe log the header value inside the for loop.

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

QUESTION

How To Handle White Space in Forloop from Dynamic Command to Create Directories

Asked 2021-Aug-06 at 16:07

So I have a for loop that spits out the information I want (A list of OpenStack Projects), and I want it to make directories for me. However, sometimes, people name their projects with whitespaces in between and I need to account for it. Here is an example of the output from my command:

1openstack project list
2
1openstack project list
2+----------------------------------+-----------------------------------------+
3| ID                               | Name                                    |
4+----------------------------------+-----------------------------------------+
5| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
6| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
7| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
8| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
9| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
10| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
11| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
12| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
13| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
14| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
15| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
16| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
17| 786b43df2b79470ea14da7468f9df1cc | service                                 |
18| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
19| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
20| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
21| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
22| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
23| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
24| cb41e6dea9744744a304c4ab77719062 | Development                             |
25| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
26| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
27| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
28+----------------------------------+-----------------------------------------+
29

My command allows for me to actually specify the names only for this:

1openstack project list
2+----------------------------------+-----------------------------------------+
3| ID                               | Name                                    |
4+----------------------------------+-----------------------------------------+
5| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
6| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
7| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
8| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
9| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
10| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
11| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
12| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
13| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
14| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
15| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
16| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
17| 786b43df2b79470ea14da7468f9df1cc | service                                 |
18| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
19| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
20| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
21| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
22| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
23| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
24| cb41e6dea9744744a304c4ab77719062 | Development                             |
25| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
26| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
27| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
28+----------------------------------+-----------------------------------------+
29openstack project list -c Name -f value
30

Which looks like this:

1openstack project list
2+----------------------------------+-----------------------------------------+
3| ID                               | Name                                    |
4+----------------------------------+-----------------------------------------+
5| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
6| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
7| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
8| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
9| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
10| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
11| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
12| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
13| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
14| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
15| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
16| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
17| 786b43df2b79470ea14da7468f9df1cc | service                                 |
18| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
19| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
20| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
21| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
22| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
23| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
24| cb41e6dea9744744a304c4ab77719062 | Development                             |
25| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
26| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
27| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
28+----------------------------------+-----------------------------------------+
29openstack project list -c Name -f value
30testing_grounds
31Michaud
32Ansible Automation
33Sustaining Development
34Data Center Services
35MF-DevStack
36Kubernetes Labs
37mso-ems
38Network Development
39Data Engineering
40Data Center Operations Service Scanning
41admin
42service
43OpenStack-Example
44Petes-place
45Image Factory
46Network Automation
47Fault and Performance Management
48Microseg
49Development
50Docker Enterprise
51Application Architecture
52casa-de-pedro
53

So I thought about making a script that would collect all of the resources per project and sotre the resources in a directory labeled by the project name. So I thought about wrapping a for loop to make directories named after the project names. I have tried the following and they have not worked:

1openstack project list
2+----------------------------------+-----------------------------------------+
3| ID                               | Name                                    |
4+----------------------------------+-----------------------------------------+
5| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
6| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
7| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
8| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
9| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
10| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
11| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
12| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
13| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
14| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
15| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
16| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
17| 786b43df2b79470ea14da7468f9df1cc | service                                 |
18| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
19| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
20| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
21| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
22| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
23| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
24| cb41e6dea9744744a304c4ab77719062 | Development                             |
25| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
26| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
27| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
28+----------------------------------+-----------------------------------------+
29openstack project list -c Name -f value
30testing_grounds
31Michaud
32Ansible Automation
33Sustaining Development
34Data Center Services
35MF-DevStack
36Kubernetes Labs
37mso-ems
38Network Development
39Data Engineering
40Data Center Operations Service Scanning
41admin
42service
43OpenStack-Example
44Petes-place
45Image Factory
46Network Automation
47Fault and Performance Management
48Microseg
49Development
50Docker Enterprise
51Application Architecture
52casa-de-pedro
53for i in $(openstack project list -c Name -f value); do mkdir -p -- 'echo $i'; done
54for i in $(openstack project list -c Name -f value); do mkdir -p -- "$i"; done
55for i in $(openstack project list -c Name -f value); do mkdir -p -- $i; done
56

none of them seem to really work and instead of incapsulating it in a string it doesn't work and instead still splits the names apart like Network Development becomes Network and Development. How can I get this to work so it will account for any whitespace that might exist within the project names.

ANSWER

Answered 2021-Aug-06 at 15:04

So unfortunately I couldn't use the regular for-loop syntax to achieve this. Instead I had to use a while loop (many thanks to Jetchisel on helping me acheive this outcome). The following method worked for me:

1openstack project list
2+----------------------------------+-----------------------------------------+
3| ID                               | Name                                    |
4+----------------------------------+-----------------------------------------+
5| 0c1abff4e920448ba86638bd0d78f7ca | testing_grounds                         |
6| 0dc5bed65a6b4a168c7531b1b4972e95 | Michaud                                 |
7| 0eab4c57a6f34c01a0de65f433c1539c | Ansible Automation                      |
8| 13d98b9d994c40029dcd2e9e4e38a26d | Sustaining Development                  |
9| 2ec59a3a665946f4b9d5098d8562ebbb | Data Center Services                    |
10| 379e46cbe60d420f977bf939ceed6961 | MF-DevStack                             |
11| 63cecde0c0384e18a949247f03b8c490 | Kubernetes Labs                         |
12| 677edd03e4ce439d884b017db19efd03 | mso-ems                                 |
13| 6865e32d9f8646b6aa449f440f1a1bb1 | Network Development                     |
14| 6a239dc6ce9c4fdca4ba2ef38849fa3b | Data Engineering                        |
15| 71ceb6e4c5f14f168ec18c069b636c02 | Data Center Operations Service Scanning |
16| 785f0b62484549048e6772e058bb0eb6 | admin                                   |
17| 786b43df2b79470ea14da7468f9df1cc | service                                 |
18| 7ca98362cb98428e9ad94c880f3efcae | OpenStack-Example                       |
19| 89498a55aa374fb4915c5a08b903fc23 | Petes-place                             |
20| 92c3fabbddb54e1696cffa223655c5d9 | Image Factory                           |
21| 9f342bd754e748e7a20bd383eab9f47d | Network Automation                      |
22| bdcba2785f334af5a2379f79d77f1308 | Fault and Performance Management        |
23| cab8e3e810c24f29ace13ca3771cc41c | Microseg                                |
24| cb41e6dea9744744a304c4ab77719062 | Development                             |
25| ccf5970b1aa74f9ebb7a542b40110462 | Docker Enterprise                       |
26| eb7d98031ec0407fb208b537a6888be5 | Application Architecture                |
27| f33c8dd6acfc467aa05215a5fe8e7798 | casa-de-pedro                           |
28+----------------------------------+-----------------------------------------+
29openstack project list -c Name -f value
30testing_grounds
31Michaud
32Ansible Automation
33Sustaining Development
34Data Center Services
35MF-DevStack
36Kubernetes Labs
37mso-ems
38Network Development
39Data Engineering
40Data Center Operations Service Scanning
41admin
42service
43OpenStack-Example
44Petes-place
45Image Factory
46Network Automation
47Fault and Performance Management
48Microseg
49Development
50Docker Enterprise
51Application Architecture
52casa-de-pedro
53for i in $(openstack project list -c Name -f value); do mkdir -p -- 'echo $i'; done
54for i in $(openstack project list -c Name -f value); do mkdir -p -- "$i"; done
55for i in $(openstack project list -c Name -f value); do mkdir -p -- $i; done
56[openstack_backup]$ while IFS= read -r name; do mkdir -p "$name"; done < <(openstack project list -c Name -f value)
57[openstack_backup]$ ls
58admin                     Data Center Operations Service Scanning  Docker Enterprise                 MF-DevStack  Network Automation   service
59Ansible Automation        Data Center Services                     Fault and Performance Management  Michaud      Network Development  Sustaining Development
60Application Architecture  Data Engineering                         Image Factory                     Microseg     OpenStack-Example    testing_grounds
61casa-de-pedro             Development                              Kubernetes Labs                   mso-ems      Petes-place
62

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

QUESTION

How to Account for White Space and Append Strings in Double Quotes in Bash Script

Asked 2021-Aug-05 at 20:43

Hi so I have a task whereby which I need to move over projects from one cluster to another. The only way I can do that is if I re-create the projects. I have a script that works and gathers the listed projects within a running OpenStack cluster, but I need to be able to take the file I store out and use it to re-create the projects.

Here is the script that gathers the available projects:

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR="/tmp/openstack_migrations"
26OUTPUTFILE="$OUTPUTDIR/projects_listed_names_as_values.txt"
27
28eval $AUTH
29echo "======================================================="
30echo "              GATHERING PROJECT INFORMATION    "
31echo "======================================================="
32# Make Directories in $OUTPUTDIR
33if [ -d "$OUTPUTDIR" ]; then
34  # Take action if $OUTPUTDIR exists
35  echo "The Directory $OUTPUTDIR exists..."
36  echo "Check for Pre-Existing OpenStack Migration Info..."
37else
38  echo "Directory $OUTPUTDIR does not exist..."
39  echo "Making $OUTPUTDIR for OpenStack Info..."
40  mkdir -p $OUTPUTDIR
41fi
42echo "Sourced Keystone via $AUTH succesful..."
43echo "======================================================="
44echo "              OPENSTACK PROJECTS COLLECTED     "
45echo "======================================================="
46openstack project list -c Name -f value
47openstack project list -c Name -f value > $OUTPUTFILE
48echo "======================================================="
49printf "\n"
50echo "Information on projects gathered has been stored in $OUTPUTFILE"
51

The $OUTPUTFILE holds contents that look like this:

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR="/tmp/openstack_migrations"
26OUTPUTFILE="$OUTPUTDIR/projects_listed_names_as_values.txt"
27
28eval $AUTH
29echo "======================================================="
30echo "              GATHERING PROJECT INFORMATION    "
31echo "======================================================="
32# Make Directories in $OUTPUTDIR
33if [ -d "$OUTPUTDIR" ]; then
34  # Take action if $OUTPUTDIR exists
35  echo "The Directory $OUTPUTDIR exists..."
36  echo "Check for Pre-Existing OpenStack Migration Info..."
37else
38  echo "Directory $OUTPUTDIR does not exist..."
39  echo "Making $OUTPUTDIR for OpenStack Info..."
40  mkdir -p $OUTPUTDIR
41fi
42echo "Sourced Keystone via $AUTH succesful..."
43echo "======================================================="
44echo "              OPENSTACK PROJECTS COLLECTED     "
45echo "======================================================="
46openstack project list -c Name -f value
47openstack project list -c Name -f value > $OUTPUTFILE
48echo "======================================================="
49printf "\n"
50echo "Information on projects gathered has been stored in $OUTPUTFILE"
51[ OpenStack]$ cat /tmp/openstack_migrations/projects_listed_names_as_values.txt
52testing_grounds
53Michaud
54Ansible Automation
55Sustaining Development
56Data Center Services
57MF-DevStack
58Kubernetes Labs
59mso-ems
60Network Development
61Data Engineering
62Data Center Operations Service Scanning
63admin
64service
65OpenStack-Example
66Petes-place
67test
68Image Factory
69Network Automation
70Fault and Performance Management
71Microseg
72Development
73Docker Enterprise
74Application Architecture
75casa-de-pedro
76

Some of the projects have white spaces. However, I thought that with a for loop this would be easy to re-create.

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR="/tmp/openstack_migrations"
26OUTPUTFILE="$OUTPUTDIR/projects_listed_names_as_values.txt"
27
28eval $AUTH
29echo "======================================================="
30echo "              GATHERING PROJECT INFORMATION    "
31echo "======================================================="
32# Make Directories in $OUTPUTDIR
33if [ -d "$OUTPUTDIR" ]; then
34  # Take action if $OUTPUTDIR exists
35  echo "The Directory $OUTPUTDIR exists..."
36  echo "Check for Pre-Existing OpenStack Migration Info..."
37else
38  echo "Directory $OUTPUTDIR does not exist..."
39  echo "Making $OUTPUTDIR for OpenStack Info..."
40  mkdir -p $OUTPUTDIR
41fi
42echo "Sourced Keystone via $AUTH succesful..."
43echo "======================================================="
44echo "              OPENSTACK PROJECTS COLLECTED     "
45echo "======================================================="
46openstack project list -c Name -f value
47openstack project list -c Name -f value > $OUTPUTFILE
48echo "======================================================="
49printf "\n"
50echo "Information on projects gathered has been stored in $OUTPUTFILE"
51[ OpenStack]$ cat /tmp/openstack_migrations/projects_listed_names_as_values.txt
52testing_grounds
53Michaud
54Ansible Automation
55Sustaining Development
56Data Center Services
57MF-DevStack
58Kubernetes Labs
59mso-ems
60Network Development
61Data Engineering
62Data Center Operations Service Scanning
63admin
64service
65OpenStack-Example
66Petes-place
67test
68Image Factory
69Network Automation
70Fault and Performance Management
71Microseg
72Development
73Docker Enterprise
74Application Architecture
75casa-de-pedro
76for i in $(cat test.txt); do openstack project create $i; done
77

However, this did not work as when I used $i it did not account for white space, so essentially any strings that had spaces in between accounted for two strings. This means that when I iterated over it and tried to re-create it, it would take Ansible Automation and try to make a project for both Ansible and Automation. To handle this I can place the strings in double quotes

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR="/tmp/openstack_migrations"
26OUTPUTFILE="$OUTPUTDIR/projects_listed_names_as_values.txt"
27
28eval $AUTH
29echo "======================================================="
30echo "              GATHERING PROJECT INFORMATION    "
31echo "======================================================="
32# Make Directories in $OUTPUTDIR
33if [ -d "$OUTPUTDIR" ]; then
34  # Take action if $OUTPUTDIR exists
35  echo "The Directory $OUTPUTDIR exists..."
36  echo "Check for Pre-Existing OpenStack Migration Info..."
37else
38  echo "Directory $OUTPUTDIR does not exist..."
39  echo "Making $OUTPUTDIR for OpenStack Info..."
40  mkdir -p $OUTPUTDIR
41fi
42echo "Sourced Keystone via $AUTH succesful..."
43echo "======================================================="
44echo "              OPENSTACK PROJECTS COLLECTED     "
45echo "======================================================="
46openstack project list -c Name -f value
47openstack project list -c Name -f value > $OUTPUTFILE
48echo "======================================================="
49printf "\n"
50echo "Information on projects gathered has been stored in $OUTPUTFILE"
51[ OpenStack]$ cat /tmp/openstack_migrations/projects_listed_names_as_values.txt
52testing_grounds
53Michaud
54Ansible Automation
55Sustaining Development
56Data Center Services
57MF-DevStack
58Kubernetes Labs
59mso-ems
60Network Development
61Data Engineering
62Data Center Operations Service Scanning
63admin
64service
65OpenStack-Example
66Petes-place
67test
68Image Factory
69Network Automation
70Fault and Performance Management
71Microseg
72Development
73Docker Enterprise
74Application Architecture
75casa-de-pedro
76for i in $(cat test.txt); do openstack project create $i; done
77openstack project create "Ansible Automation"
78

This would create the project. How can I account for space within the text file and append my strings inside double quotes using a for-loop in bash?

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR="/tmp/openstack_migrations"
26OUTPUTFILE="$OUTPUTDIR/projects_listed_names_as_values.txt"
27
28eval $AUTH
29echo "======================================================="
30echo "              GATHERING PROJECT INFORMATION    "
31echo "======================================================="
32# Make Directories in $OUTPUTDIR
33if [ -d "$OUTPUTDIR" ]; then
34  # Take action if $OUTPUTDIR exists
35  echo "The Directory $OUTPUTDIR exists..."
36  echo "Check for Pre-Existing OpenStack Migration Info..."
37else
38  echo "Directory $OUTPUTDIR does not exist..."
39  echo "Making $OUTPUTDIR for OpenStack Info..."
40  mkdir -p $OUTPUTDIR
41fi
42echo "Sourced Keystone via $AUTH succesful..."
43echo "======================================================="
44echo "              OPENSTACK PROJECTS COLLECTED     "
45echo "======================================================="
46openstack project list -c Name -f value
47openstack project list -c Name -f value > $OUTPUTFILE
48echo "======================================================="
49printf "\n"
50echo "Information on projects gathered has been stored in $OUTPUTFILE"
51[ OpenStack]$ cat /tmp/openstack_migrations/projects_listed_names_as_values.txt
52testing_grounds
53Michaud
54Ansible Automation
55Sustaining Development
56Data Center Services
57MF-DevStack
58Kubernetes Labs
59mso-ems
60Network Development
61Data Engineering
62Data Center Operations Service Scanning
63admin
64service
65OpenStack-Example
66Petes-place
67test
68Image Factory
69Network Automation
70Fault and Performance Management
71Microseg
72Development
73Docker Enterprise
74Application Architecture
75casa-de-pedro
76for i in $(cat test.txt); do openstack project create $i; done
77openstack project create "Ansible Automation"
78for i in $(cat file.txt); do openstack project create <something with $i that places the string with white space in double quotes "<string>">; done
79

Can someone help me figure out how to account for "<string1 string2>"

ANSWER

Answered 2021-Aug-05 at 20:43

You can also use mapfile for an array.

1#!/bin/bash
2# ======================================================
3# Author: Richard Barrett
4# Date Created: 08/05/2021
5# Organization: ITAdminInfra
6# Purpose: Initialize OpenStack Project Gather Script
7# ======================================================
8
9start=`date +%s`
10#set -e
11
12# keep track of the last executed command
13trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
14
15# echo an error message before exiting
16#trap 'echo &quot;\&quot;${last_command}\&quot; command filed with exit code $?.&quot;' EXIT
17
18# EXIT Codes
19# ==========
20EXIT=1
21EXIT_2=2
22EXIT_126=126
23
24AUTH=$(source ~/admin-openrc.sh)
25OUTPUTDIR=&quot;/tmp/openstack_migrations&quot;
26OUTPUTFILE=&quot;$OUTPUTDIR/projects_listed_names_as_values.txt&quot;
27
28eval $AUTH
29echo &quot;=======================================================&quot;
30echo &quot;              GATHERING PROJECT INFORMATION    &quot;
31echo &quot;=======================================================&quot;
32# Make Directories in $OUTPUTDIR
33if [ -d &quot;$OUTPUTDIR&quot; ]; then
34  # Take action if $OUTPUTDIR exists
35  echo &quot;The Directory $OUTPUTDIR exists...&quot;
36  echo &quot;Check for Pre-Existing OpenStack Migration Info...&quot;
37else
38  echo &quot;Directory $OUTPUTDIR does not exist...&quot;
39  echo &quot;Making $OUTPUTDIR for OpenStack Info...&quot;
40  mkdir -p $OUTPUTDIR
41fi
42echo &quot;Sourced Keystone via $AUTH succesful...&quot;
43echo &quot;=======================================================&quot;
44echo &quot;              OPENSTACK PROJECTS COLLECTED     &quot;
45echo &quot;=======================================================&quot;
46openstack project list -c Name -f value
47openstack project list -c Name -f value &gt; $OUTPUTFILE
48echo &quot;=======================================================&quot;
49printf &quot;\n&quot;
50echo &quot;Information on projects gathered has been stored in $OUTPUTFILE&quot;
51[ OpenStack]$ cat /tmp/openstack_migrations/projects_listed_names_as_values.txt
52testing_grounds
53Michaud
54Ansible Automation
55Sustaining Development
56Data Center Services
57MF-DevStack
58Kubernetes Labs
59mso-ems
60Network Development
61Data Engineering
62Data Center Operations Service Scanning
63admin
64service
65OpenStack-Example
66Petes-place
67test
68Image Factory
69Network Automation
70Fault and Performance Management
71Microseg
72Development
73Docker Enterprise
74Application Architecture
75casa-de-pedro
76for i in $(cat test.txt); do openstack project create $i; done
77openstack project create &quot;Ansible Automation&quot;
78for i in $(cat file.txt); do openstack project create &lt;something with $i that places the string with white space in double quotes &quot;&lt;string&gt;&quot;&gt;; done
79mapfile -t prj &lt; file
80for p in &quot;${prj[@]}&quot;; do echo &quot;[$p]&quot;; done
81

This should correctly quote your project names.
Extrapolate for your needs.

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

QUESTION

&quot;statistics unavailable at this time&quot; when trying to print String Pool information?

Asked 2021-Jan-04 at 20:35

I am following a udemy course on java performance management and I am coming across an issue that is preventing me from printing out information for the String pool. This is what I get when I attempt to add ten million String objects to the pool:

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14

This is what I get when I run the program with an empty main method:

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33

This is the code that attempts to add 10 million Strings to the pool:

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55

So, my question is: Why can't I print out information for the String pool when I attempt to add 10 million objects, but my instructor can? I am failing to understand the reasoning as this is pretty beyond my level of Java programming. Thank you all.

ANSWER

Answered 2021-Jan-04 at 03:42
tl;dr

Wait a few seconds before exiting, to avoid "statistics unavailable at this moment" issue.

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56
Details

If you are referring to that message on the console:

statistics unavailable at this moment

… I experienced the same with code similar to yours.

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56package work.basil.example;
57
58import java.time.Duration;
59import java.time.Instant;
60import java.util.ArrayList;
61import java.util.List;
62
63public class Stringer
64{
65    public static void main ( String[] args )
66    {
67        Instant start = Instant.now();
68        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
69        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
70
71        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
72        {
73            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
74            strings.add( s );
75        }
76
77        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
78    }
79}
80

When run on macOS Mojave on Intel with Java 15 from AdoptOpenJDK, executed within IntelliJ 2020.3.1.

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56package work.basil.example;
57
58import java.time.Duration;
59import java.time.Instant;
60import java.util.ArrayList;
61import java.util.List;
62
63public class Stringer
64{
65    public static void main ( String[] args )
66    {
67        Instant start = Instant.now();
68        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
69        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
70
71        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
72        {
73            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
74            strings.add( s );
75        }
76
77        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
78    }
79}
80/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/java -XX:+PrintStringTableStatistics --enable-preview -javaagent:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/lib/idea_rt.jar=52178:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/basilbourque/IdeaProjects/ExampleJava/target/classes:/Users/basilbourque/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/Users/basilbourque/.m2/repository/com/thedeanda/lorem/2.1/lorem-2.1.jar:/Users/basilbourque/.m2/repository/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar:/Users/basilbourque/.m2/repository/org/checkerframework/checker-qual/3.5.0/checker-qual-3.5.0.jar:/Users/basilbourque/.m2/repository/org/apache/commons/commons-csv/1.8/commons-csv-1.8.jar work.basil.example.Stringer
81Starting run at:2021-01-04T02:03:34.484633Z. Runtime.version(): 15.0.1+9
82Elapsed time was PT6.219106S
83SymbolTable statistics:
84Number of buckets       :     32768 =    262144 bytes, each 8
85Number of entries       :      2633 =     42128 bytes, each 16
86Number of literals      :      2633 =     98200 bytes, avg  37.000
87Total footprint         :           =    402472 bytes
88Average bucket size     :     0.080
89Variance of bucket size :     0.079
90Std. dev. of bucket size:     0.280
91Maximum bucket size     :         2
92statistics unavailable at this moment
93
94Process finished with exit code 0
95

…where I get the same statistics unavailable at this moment message.

So on a wild guess, I added a Thread.sleep call to wait 5 seconds before exiting.

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56package work.basil.example;
57
58import java.time.Duration;
59import java.time.Instant;
60import java.util.ArrayList;
61import java.util.List;
62
63public class Stringer
64{
65    public static void main ( String[] args )
66    {
67        Instant start = Instant.now();
68        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
69        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
70
71        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
72        {
73            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
74            strings.add( s );
75        }
76
77        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
78    }
79}
80/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/java -XX:+PrintStringTableStatistics --enable-preview -javaagent:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/lib/idea_rt.jar=52178:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/basilbourque/IdeaProjects/ExampleJava/target/classes:/Users/basilbourque/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/Users/basilbourque/.m2/repository/com/thedeanda/lorem/2.1/lorem-2.1.jar:/Users/basilbourque/.m2/repository/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar:/Users/basilbourque/.m2/repository/org/checkerframework/checker-qual/3.5.0/checker-qual-3.5.0.jar:/Users/basilbourque/.m2/repository/org/apache/commons/commons-csv/1.8/commons-csv-1.8.jar work.basil.example.Stringer
81Starting run at:2021-01-04T02:03:34.484633Z. Runtime.version(): 15.0.1+9
82Elapsed time was PT6.219106S
83SymbolTable statistics:
84Number of buckets       :     32768 =    262144 bytes, each 8
85Number of entries       :      2633 =     42128 bytes, each 16
86Number of literals      :      2633 =     98200 bytes, avg  37.000
87Total footprint         :           =    402472 bytes
88Average bucket size     :     0.080
89Variance of bucket size :     0.079
90Std. dev. of bucket size:     0.280
91Maximum bucket size     :         2
92statistics unavailable at this moment
93
94Process finished with exit code 0
95try { Thread.sleep( Duration.ofSeconds( 5 ).toMillis() ); } catch ( InterruptedException e ) { e.printStackTrace(); }
96

So, in running this:

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56package work.basil.example;
57
58import java.time.Duration;
59import java.time.Instant;
60import java.util.ArrayList;
61import java.util.List;
62
63public class Stringer
64{
65    public static void main ( String[] args )
66    {
67        Instant start = Instant.now();
68        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
69        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
70
71        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
72        {
73            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
74            strings.add( s );
75        }
76
77        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
78    }
79}
80/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/java -XX:+PrintStringTableStatistics --enable-preview -javaagent:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/lib/idea_rt.jar=52178:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/basilbourque/IdeaProjects/ExampleJava/target/classes:/Users/basilbourque/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/Users/basilbourque/.m2/repository/com/thedeanda/lorem/2.1/lorem-2.1.jar:/Users/basilbourque/.m2/repository/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar:/Users/basilbourque/.m2/repository/org/checkerframework/checker-qual/3.5.0/checker-qual-3.5.0.jar:/Users/basilbourque/.m2/repository/org/apache/commons/commons-csv/1.8/commons-csv-1.8.jar work.basil.example.Stringer
81Starting run at:2021-01-04T02:03:34.484633Z. Runtime.version(): 15.0.1+9
82Elapsed time was PT6.219106S
83SymbolTable statistics:
84Number of buckets       :     32768 =    262144 bytes, each 8
85Number of entries       :      2633 =     42128 bytes, each 16
86Number of literals      :      2633 =     98200 bytes, avg  37.000
87Total footprint         :           =    402472 bytes
88Average bucket size     :     0.080
89Variance of bucket size :     0.079
90Std. dev. of bucket size:     0.280
91Maximum bucket size     :         2
92statistics unavailable at this moment
93
94Process finished with exit code 0
95try { Thread.sleep( Duration.ofSeconds( 5 ).toMillis() ); } catch ( InterruptedException e ) { e.printStackTrace(); }
96package work.basil.example;
97
98import java.time.Duration;
99import java.time.Instant;
100import java.util.ArrayList;
101import java.util.List;
102
103public class Stringer
104{
105    public static void main ( String[] args )
106    {
107        Instant start = Instant.now();
108        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
109        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
110
111        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
112        {
113            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
114            strings.add( s );
115        }
116
117        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
118        try { Thread.sleep( Duration.ofSeconds( 5 ).toMillis() ); } catch ( InterruptedException e ) { e.printStackTrace(); }
119    }
120}
121

I get this:

1E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
2Elapsed time was 28311 ms.
3SymbolTable statistics:
4Number of buckets       :     20011 =    160088 bytes, each 8
5Number of entries       :     20527 =    492648 bytes, each 24
6Number of literals      :     20527 =    776904 bytes, avg  37.848
7Total footprint         :           =   1429640 bytes
8Average bucket size     :     1.026
9Variance of bucket size :     1.033
10Std. dev. of bucket size:     1.017
11Maximum bucket size     :         9
12statistics unavailable at this moment
13
14E:\IntelliJ Projects\ExploringStrings\src&gt;java -XX:+PrintStringTableStatistics Main
15SymbolTable statistics:
16Number of buckets       :     20011 =    160088 bytes, each 8
17Number of entries       :     16615 =    398760 bytes, each 24
18Number of literals      :     16615 =    608976 bytes, avg  36.652
19Total footprint         :           =   1167824 bytes
20Average bucket size     :     0.830
21Variance of bucket size :     0.837
22Std. dev. of bucket size:     0.915
23Maximum bucket size     :         9
24StringTable statistics:
25Number of buckets       :     65536 =    524288 bytes, each 8
26Number of entries       :      1734 =     27744 bytes, each 16
27Number of literals      :      1734 =    112456 bytes, avg  64.854
28Total footprsize_t         :           =    664488 bytes
29Average bucket size     :     0.026
30Variance of bucket size :     0.027
31Std. dev. of bucket size:     0.163
32Maximum bucket size     :         2
33import java.util.ArrayList;
34import java.util.Date;
35import java.util.List;
36
37public class Main {
38
39    public static void main(String[] args) {
40
41        Date start = new Date();
42
43        List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
44
45        for(Integer i = 1; i &lt; 10000000; i++) {
46            String s =  i.toString().intern(); //Adds new string object to the string pool
47            strings.add(s);
48        }
49
50        Date end = new Date();
51        System.out.println(&quot;Elapsed time was &quot; + (end.getTime() - start.getTime()) + &quot; ms.&quot;);
52
53    }
54}
55Thread.sleep( Duration.ofSeconds( 5 ).toMillis() )
56package work.basil.example;
57
58import java.time.Duration;
59import java.time.Instant;
60import java.util.ArrayList;
61import java.util.List;
62
63public class Stringer
64{
65    public static void main ( String[] args )
66    {
67        Instant start = Instant.now();
68        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
69        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
70
71        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
72        {
73            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
74            strings.add( s );
75        }
76
77        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
78    }
79}
80/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/java -XX:+PrintStringTableStatistics --enable-preview -javaagent:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/lib/idea_rt.jar=52178:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/basilbourque/IdeaProjects/ExampleJava/target/classes:/Users/basilbourque/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/Users/basilbourque/.m2/repository/com/thedeanda/lorem/2.1/lorem-2.1.jar:/Users/basilbourque/.m2/repository/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar:/Users/basilbourque/.m2/repository/org/checkerframework/checker-qual/3.5.0/checker-qual-3.5.0.jar:/Users/basilbourque/.m2/repository/org/apache/commons/commons-csv/1.8/commons-csv-1.8.jar work.basil.example.Stringer
81Starting run at:2021-01-04T02:03:34.484633Z. Runtime.version(): 15.0.1+9
82Elapsed time was PT6.219106S
83SymbolTable statistics:
84Number of buckets       :     32768 =    262144 bytes, each 8
85Number of entries       :      2633 =     42128 bytes, each 16
86Number of literals      :      2633 =     98200 bytes, avg  37.000
87Total footprint         :           =    402472 bytes
88Average bucket size     :     0.080
89Variance of bucket size :     0.079
90Std. dev. of bucket size:     0.280
91Maximum bucket size     :         2
92statistics unavailable at this moment
93
94Process finished with exit code 0
95try { Thread.sleep( Duration.ofSeconds( 5 ).toMillis() ); } catch ( InterruptedException e ) { e.printStackTrace(); }
96package work.basil.example;
97
98import java.time.Duration;
99import java.time.Instant;
100import java.util.ArrayList;
101import java.util.List;
102
103public class Stringer
104{
105    public static void main ( String[] args )
106    {
107        Instant start = Instant.now();
108        System.out.println( &quot;Starting run at:&quot; + start + &quot;. Runtime.version(): &quot; + Runtime.version() );
109        List &lt; String &gt; strings = new ArrayList &lt; String &gt;();
110
111        for ( int i = 1 ; i &lt; 10_000_000 ; i++ )
112        {
113            String s = String.valueOf( i ).intern();  // Adds new string object to the string pool.
114            strings.add( s );
115        }
116
117        System.out.println( &quot;Elapsed time was &quot; + Duration.between( start , Instant.now() ) );
118        try { Thread.sleep( Duration.ofSeconds( 5 ).toMillis() ); } catch ( InterruptedException e ) { e.printStackTrace(); }
119    }
120}
121/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/java -XX:+PrintStringTableStatistics --enable-preview -javaagent:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/lib/idea_rt.jar=52114:/Users/basilbourque/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.6682.168/IntelliJ IDEA 2020.3 EAP.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/basilbourque/IdeaProjects/ExampleJava/target/classes:/Users/basilbourque/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar:/Users/basilbourque/.m2/repository/com/thedeanda/lorem/2.1/lorem-2.1.jar:/Users/basilbourque/.m2/repository/org/postgresql/postgresql/42.2.18/postgresql-42.2.18.jar:/Users/basilbourque/.m2/repository/org/checkerframework/checker-qual/3.5.0/checker-qual-3.5.0.jar:/Users/basilbourque/.m2/repository/org/apache/commons/commons-csv/1.8/commons-csv-1.8.jar work.basil.example.Stringer
122Starting run at:2021-01-04T02:02:21.792377Z. Runtime.version(): 15.0.1+9
123Elapsed time was PT5.887848S
124SymbolTable statistics:
125Number of buckets       :     32768 =    262144 bytes, each 8
126Number of entries       :      2635 =     42160 bytes, each 16
127Number of literals      :      2635 =     98280 bytes, avg  37.000
128Total footprint         :           =    402584 bytes
129Average bucket size     :     0.080
130Variance of bucket size :     0.079
131Std. dev. of bucket size:     0.281
132Maximum bucket size     :         2
133StringTable statistics:
134Number of buckets       :   8388608 =  67108864 bytes, each 8
135Number of entries       :  10000880 = 160014080 bytes, each 16
136Number of literals      :  10000880 = 480062480 bytes, avg  48.000
137Total footprint         :           = 707185424 bytes
138Average bucket size     :     1.192
139Variance of bucket size :     1.383
140Std. dev. of bucket size:     1.176
141Maximum bucket size     :         9
142
143Process finished with exit code 0
144

After waiting to exit, the statistics unavailable at this moment message is gone, and we get full statistics.

We do indeed see Number of literals jump from a few thousand to over ten million. Presumably this is due to our loop of ten million numbers turned into strings.

Documentation?

I tried to research this, to learn the documented behavior of the PrintStringTableStatistics switch. But I cannot find any documentation. I have no idea where to find documentation nowadays for the console tools java, javac, and so on.

If anyone has a link, please post.

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

QUESTION

How to combine array of data and make is more easy to use

Asked 2020-Dec-22 at 12:46

I am working with a filter that I can use after the search so that way I created a table in the database and insert some category and its sub categories and now I want to combine all of the to make a tree like structure. But I am stuck with this

Now How can I achieve this?

1[
2  {
3    "parent_category": "Recruitment",
4    "category_name": "Job description"
5  },
6  {
7    "parent_category": "Recruitment",
8    "category_name": "Forms"
9  },
10   {
11    "parent_category": "Performance management system",
12    "category_name": "Job description"
13  },
14  {
15    "parent_category": "Exit",
16    "category_name": "Job description"
17  }
18  ]

to this type of result :

1[
2  {
3    "parent_category": "Recruitment",
4    "category_name": "Job description"
5  },
6  {
7    "parent_category": "Recruitment",
8    "category_name": "Forms"
9  },
10   {
11    "parent_category": "Performance management system",
12    "category_name": "Job description"
13  },
14  {
15    "parent_category": "Exit",
16    "category_name": "Job description"
17  }
18  ][
19  {
20    "parent_category": "Recruitment",
21    "category_name": "Job description, Forms"
22  },
23   {
24    "parent_category": "Performance management system",
25    "category_name": "Job description"
26  },
27  {
28    "parent_category": "Exit",
29    "category_name": "Job description"
30  }
31  ]

Without using a long type of for loop because it consume more time.

ANSWER

Answered 2020-Dec-22 at 12:38

You can do something like this:

1[
2  {
3    "parent_category": "Recruitment",
4    "category_name": "Job description"
5  },
6  {
7    "parent_category": "Recruitment",
8    "category_name": "Forms"
9  },
10   {
11    "parent_category": "Performance management system",
12    "category_name": "Job description"
13  },
14  {
15    "parent_category": "Exit",
16    "category_name": "Job description"
17  }
18  ][
19  {
20    "parent_category": "Recruitment",
21    "category_name": "Job description, Forms"
22  },
23   {
24    "parent_category": "Performance management system",
25    "category_name": "Job description"
26  },
27  {
28    "parent_category": "Exit",
29    "category_name": "Job description"
30  }
31  ]$result = [];
32
33foreach ($categories as $category) {
34    if (!isset($result[$category-&gt;parent_category])) {
35        $result[$category-&gt;parent_category] = $category;
36    } else {
37        $result[$category-&gt;parent_category]-&gt;category_name .= ', ' . $category-&gt;category_name;
38    }
39}
40
41echo json_encode(array_values($result));
42

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

QUESTION

How to use update method in viewsets in Django Rest Framework and perform some task(like sending mail) along with it?

Asked 2020-Jul-23 at 12:25

I am working on a simple performance management system with react on frontend and django on the backend. They are supervisors who can give reviews to supervisees and supervisees can respond. I want all employees to receive email when they receive reviews from their supervisors and all the supervisors to receive email when their reviews are responded. For reviews and responses I am using two different serializers but same model.

Serializer for Response is:

1class ResponseSerializer(serializers.ModelSerializer):
2    supervisor_name = serializers.SerializerMethodField('get_supervisor_name')
3    supervisor_email = serializers.SerializerMethodField('get_supervisor_email')
4    supervisee_name = serializers.SerializerMethodField('get_supervisee_name')
5    supervisee_email = serializers.SerializerMethodField('get_supervisee_email')
6
7    class Meta:
8        model = Review
9        fields = (
10            'id', 'review_text', 'response_text', 'date_of_review', 'date_of_response', 'supervisor', 'supervisor_name',
11            'supervisor_email', 'supervisee', 'supervisee_name', 'supervisee_email')
12        read_only_fields = ('review_text', 'date_of_review', 'supervisor', 'supervisee')
13
14    def get_supervisor_name(self, obj):
15        return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
16
17    def get_supervisor_email(self, obj):
18        return obj.supervisor.email
19
20    def get_supervisee_name(self, obj):
21        return obj.supervisee.first_name + &quot; &quot; + obj.supervisee.last_name
22
23    def get_supervisee_email(self, obj):
24        return obj.supervisee.email
25

For sending mail I am using send_mail method from django.core And I am using Viewsets for Reviews and Responses.

Now Response operation will always be an update operation because Response will always be used to update existing Review object in which response_text field will be updated.

1class ResponseSerializer(serializers.ModelSerializer):
2    supervisor_name = serializers.SerializerMethodField('get_supervisor_name')
3    supervisor_email = serializers.SerializerMethodField('get_supervisor_email')
4    supervisee_name = serializers.SerializerMethodField('get_supervisee_name')
5    supervisee_email = serializers.SerializerMethodField('get_supervisee_email')
6
7    class Meta:
8        model = Review
9        fields = (
10            'id', 'review_text', 'response_text', 'date_of_review', 'date_of_response', 'supervisor', 'supervisor_name',
11            'supervisor_email', 'supervisee', 'supervisee_name', 'supervisee_email')
12        read_only_fields = ('review_text', 'date_of_review', 'supervisor', 'supervisee')
13
14    def get_supervisor_name(self, obj):
15        return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
16
17    def get_supervisor_email(self, obj):
18        return obj.supervisor.email
19
20    def get_supervisee_name(self, obj):
21        return obj.supervisee.first_name + &quot; &quot; + obj.supervisee.last_name
22
23    def get_supervisee_email(self, obj):
24        return obj.supervisee.email
25class ResponseViewSet(viewsets.ModelViewSet):
26    queryset = Review.objects.all()
27    permission_classes = [
28        # permissions.IsAuthenticated,
29        permissions.AllowAny,
30    ]
31    serializer_class = ResponseSerializer
32
33    def update(self, request, *args, **kwargs):
34        serializer = ResponseSerializer(data=request.data)
35
36        if serializer.is_valid():
37            supervisor = serializer.data[&quot;supervisor_name&quot;]
38            supervisee = serializer.data[&quot;supervisee_name&quot;]
39            query = serializer.save()
40            mail_text = &quot;Hi {}\n\nYou got a response for your 1:1 from {}.\n\nClick below to see the response:\n\n{}&quot;.format(
41                supervisor,
42                supervisee,
43                &quot;https://example.com/#/pms/reviewsBySupervisor&quot;,
44            )
45            try:
46                if not settings.DEFAULT_EMAIL_RECIPIENTS:
47                    settings.DEFAULT_EMAIL_RECIPIENTS.append(
48                        str(serializer.data[&quot;supervisor_email&quot;])
49                    )
50                send_mail(
51                    subject=&quot;New Response Received&quot;,
52                    message=mail_text,
53                    from_email=settings.DEFAULT_FROM_EMAIL,
54                    recipient_list=settings.DEFAULT_EMAIL_RECIPIENTS,
55                    fail_silently=False,
56                )
57            except (SMTPRecipientsRefused, SMTPSenderRefused):
58                LOGGER.exception(&quot;There was a problem submitting the form.&quot;)
59            return Response(serializer.data, status=status.HTTP_201_CREATED)
60        else:
61            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
62

So, the problem that I am facing is that when I try to send mail with update method in ResponseViewset as shown above. I get the following error:

1class ResponseSerializer(serializers.ModelSerializer):
2    supervisor_name = serializers.SerializerMethodField('get_supervisor_name')
3    supervisor_email = serializers.SerializerMethodField('get_supervisor_email')
4    supervisee_name = serializers.SerializerMethodField('get_supervisee_name')
5    supervisee_email = serializers.SerializerMethodField('get_supervisee_email')
6
7    class Meta:
8        model = Review
9        fields = (
10            'id', 'review_text', 'response_text', 'date_of_review', 'date_of_response', 'supervisor', 'supervisor_name',
11            'supervisor_email', 'supervisee', 'supervisee_name', 'supervisee_email')
12        read_only_fields = ('review_text', 'date_of_review', 'supervisor', 'supervisee')
13
14    def get_supervisor_name(self, obj):
15        return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
16
17    def get_supervisor_email(self, obj):
18        return obj.supervisor.email
19
20    def get_supervisee_name(self, obj):
21        return obj.supervisee.first_name + &quot; &quot; + obj.supervisee.last_name
22
23    def get_supervisee_email(self, obj):
24        return obj.supervisee.email
25class ResponseViewSet(viewsets.ModelViewSet):
26    queryset = Review.objects.all()
27    permission_classes = [
28        # permissions.IsAuthenticated,
29        permissions.AllowAny,
30    ]
31    serializer_class = ResponseSerializer
32
33    def update(self, request, *args, **kwargs):
34        serializer = ResponseSerializer(data=request.data)
35
36        if serializer.is_valid():
37            supervisor = serializer.data[&quot;supervisor_name&quot;]
38            supervisee = serializer.data[&quot;supervisee_name&quot;]
39            query = serializer.save()
40            mail_text = &quot;Hi {}\n\nYou got a response for your 1:1 from {}.\n\nClick below to see the response:\n\n{}&quot;.format(
41                supervisor,
42                supervisee,
43                &quot;https://example.com/#/pms/reviewsBySupervisor&quot;,
44            )
45            try:
46                if not settings.DEFAULT_EMAIL_RECIPIENTS:
47                    settings.DEFAULT_EMAIL_RECIPIENTS.append(
48                        str(serializer.data[&quot;supervisor_email&quot;])
49                    )
50                send_mail(
51                    subject=&quot;New Response Received&quot;,
52                    message=mail_text,
53                    from_email=settings.DEFAULT_FROM_EMAIL,
54                    recipient_list=settings.DEFAULT_EMAIL_RECIPIENTS,
55                    fail_silently=False,
56                )
57            except (SMTPRecipientsRefused, SMTPSenderRefused):
58                LOGGER.exception(&quot;There was a problem submitting the form.&quot;)
59            return Response(serializer.data, status=status.HTTP_201_CREATED)
60        else:
61            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
62Internal Server Error: /UMS/api/responses/38/
63Traceback (most recent call last):
64  File &quot;/home/shishir/Projects/performance_management/performance_management/reviews/serializers.py&quot;, line 77, in get_supervisor_name
65    return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
66AttributeError: 'NoneType' object has no attribute 'first_name'
67

So, what is happening is due to some reason, all the fields of that particular review are getting set to null as soon as I try to update and hence getting NoneType object has no attribute. I have checked in database table(MySQL), all the fields are getting set to null. Can anyone tell me why is this happening ? Where am I going wrong ? And what is the correct way to do it?

ANSWER

Answered 2020-Jul-23 at 12:25

Finally I found my solution by changing the method update to partial_update. Apparently update method updates all the field while in above case I am attempting the field called response_text in the Review model which are setting other fields to null if they could be. Also after doing that I had to change the request from PUT to PATCH in frontend. Also I had to do some other minor coding changes like removing supervisor and supervisee fields from read_only_fields from ResponseSerializer. Updated code for ResponseViewset is shown below:

1class ResponseSerializer(serializers.ModelSerializer):
2    supervisor_name = serializers.SerializerMethodField('get_supervisor_name')
3    supervisor_email = serializers.SerializerMethodField('get_supervisor_email')
4    supervisee_name = serializers.SerializerMethodField('get_supervisee_name')
5    supervisee_email = serializers.SerializerMethodField('get_supervisee_email')
6
7    class Meta:
8        model = Review
9        fields = (
10            'id', 'review_text', 'response_text', 'date_of_review', 'date_of_response', 'supervisor', 'supervisor_name',
11            'supervisor_email', 'supervisee', 'supervisee_name', 'supervisee_email')
12        read_only_fields = ('review_text', 'date_of_review', 'supervisor', 'supervisee')
13
14    def get_supervisor_name(self, obj):
15        return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
16
17    def get_supervisor_email(self, obj):
18        return obj.supervisor.email
19
20    def get_supervisee_name(self, obj):
21        return obj.supervisee.first_name + &quot; &quot; + obj.supervisee.last_name
22
23    def get_supervisee_email(self, obj):
24        return obj.supervisee.email
25class ResponseViewSet(viewsets.ModelViewSet):
26    queryset = Review.objects.all()
27    permission_classes = [
28        # permissions.IsAuthenticated,
29        permissions.AllowAny,
30    ]
31    serializer_class = ResponseSerializer
32
33    def update(self, request, *args, **kwargs):
34        serializer = ResponseSerializer(data=request.data)
35
36        if serializer.is_valid():
37            supervisor = serializer.data[&quot;supervisor_name&quot;]
38            supervisee = serializer.data[&quot;supervisee_name&quot;]
39            query = serializer.save()
40            mail_text = &quot;Hi {}\n\nYou got a response for your 1:1 from {}.\n\nClick below to see the response:\n\n{}&quot;.format(
41                supervisor,
42                supervisee,
43                &quot;https://example.com/#/pms/reviewsBySupervisor&quot;,
44            )
45            try:
46                if not settings.DEFAULT_EMAIL_RECIPIENTS:
47                    settings.DEFAULT_EMAIL_RECIPIENTS.append(
48                        str(serializer.data[&quot;supervisor_email&quot;])
49                    )
50                send_mail(
51                    subject=&quot;New Response Received&quot;,
52                    message=mail_text,
53                    from_email=settings.DEFAULT_FROM_EMAIL,
54                    recipient_list=settings.DEFAULT_EMAIL_RECIPIENTS,
55                    fail_silently=False,
56                )
57            except (SMTPRecipientsRefused, SMTPSenderRefused):
58                LOGGER.exception(&quot;There was a problem submitting the form.&quot;)
59            return Response(serializer.data, status=status.HTTP_201_CREATED)
60        else:
61            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
62Internal Server Error: /UMS/api/responses/38/
63Traceback (most recent call last):
64  File &quot;/home/shishir/Projects/performance_management/performance_management/reviews/serializers.py&quot;, line 77, in get_supervisor_name
65    return obj.supervisor.first_name + &quot; &quot; + obj.supervisor.last_name
66AttributeError: 'NoneType' object has no attribute 'first_name'
67class ResponseViewSet(viewsets.ModelViewSet):
68    queryset = Review.objects.all()
69    permission_classes = [
70        permissions.IsAuthenticated,
71        #permissions.AllowAny,
72    ]
73    serializer_class = ResponseSerializer
74
75    def partial_update(self, request,*args, **kwargs):
76        obj = self.get_object()
77        serializer = self.serializer_class(obj, data=request.data, partial=True)
78        if serializer.is_valid():
79            serializer.save()
80            mail_text = &quot;Hi {},\n\nYou got a response for your 1:1 from {}.\n\nClick below to see the response:\n\n{}&quot;.format(
81                serializer.data[&quot;supervisor_name&quot;],
82                serializer.data[&quot;supervisee_name&quot;],
83                get_product_link(&quot;UMS/reviewsForDR&quot;),
84            )
85            try:
86                if not settings.DEFAULT_EMAIL_RECIPIENTS:
87                    settings.DEFAULT_EMAIL_RECIPIENTS.append(
88                        # supervisor_email
89                        serializer.data[&quot;supervisor_email&quot;]
90                    )
91
92                send_mail(
93                    subject=&quot;New Response Received&quot;,
94                    message=mail_text,
95                    from_email=settings.DEFAULT_FROM_EMAIL,
96                    recipient_list=settings.DEFAULT_EMAIL_RECIPIENTS,
97                    fail_silently=False,
98                )
99                settings.DEFAULT_EMAIL_RECIPIENTS = []
100            except (SMTPRecipientsRefused, SMTPSenderRefused):
101                LOGGER.exception(&quot;There was a problem submitting the form.&quot;)
102            return Response(serializer.data, status=status.HTTP_201_CREATED)
103        else:
104            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
105

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

QUESTION

Is using 'ref' or 'out' common practice in C#

Asked 2020-Jul-23 at 07:14

I was sticking on Unity for a while, without knowing much about Computer Science/Performance management.

Now, I took some C++ experience, with some concerns on IO/performance/etc some "Computer Engineering" stuffs.

Then I got back to my old Unity project, which had no concern about those thing at all, and found out that there is ref(or out) keyword for call-by-reference, as I could do in my C++ project.

Is it common to use ref(for call-by-reference purpose) of C# on 'business level'?(maybe game companies with Unity?)

ANSWER

Answered 2020-Jul-23 at 07:08

If you come from C++ then you have this thought in your head that you need to pass references, because if you don't, the whole object will be copied and most likely that is bad (tm).

In C#, types are either reference types or value types. Reference types are passed by reference automatically. If you pass a class, it can be null, so it must already be a reference to the actual class instance (or in C++ terminology a "pointer", because references in c++ cannot be null).

All classes are reference types. You don't need ref or out for performance reasons ever. If you find yourself using ref or out for performance reasons on a struct, that should be your clue that it should be a class instead.

So no, using ref or out is not common in C# at all. It is only used sparingly, in the places where you would not get the desired result without it. Those keywords are not meant for performance considerations.

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

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Performance Management

Tutorials and Learning Resources are not available at this moment for Performance Management

Share this Page

share link

Get latest updates on Performance Management