Programming

A place to discuss, learn, and share knowledge about programming. This is a broad community covering all aspects of programming ranging from beginner concepts to advanced topics.

codie
codie

I was working in Phoenix/Elixir and created a function component for navbar, and got this issue:Done in 38ms. Compiling 1 file (.ex) error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 55 │ href={~p"/users/log_out"} │ ^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:55: RankgistWeb.NavComponents.main/1 error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 45 │ href={~p"/track-competitors"} │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:45: RankgistWeb.NavComponents.main/1 error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 8 │ <.link href={~p"/"} class="flex items-center space-x-3 rtl:space-x-reverse"> │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:8: RankgistWeb.NavComponents.main/1 == Compilation error in file lib/rankgist_web/components/nav_components.ex == ** (CompileError) lib/rankgist_web/components/nav_components.ex: cannot compile module RankgistWeb.NavComponents (errors have been logged) Compiling 1 file (.ex) error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 55 │ href={~p"/users/log_out"} │ ^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:55: RankgistWeb.NavComponents.main/1 error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 45 │ href={~p"/track-competitors"} │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:45: RankgistWeb.NavComponents.main/1 error: undefined function sigil_p/2 (expected RankgistWeb.NavComponents to define such a function or for it to be imported, but none are available) │ 8 │ <.link href={~p"/"} class="flex items-center space-x-3 rtl:space-x-reverse"> │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ └─ (rankgist 0.1.0) lib/rankgist_web/components/nav_components.ex:8: RankgistWeb.NavComponents.main/1 == Compilation error in file lib/rankgist_web/components/nav_components.ex == ** (CompileError) lib/rankgist_web/components/nav_components.ex: cannot compile module RankgistWeb.NavComponents (errors have been logged)to fix the issue of sigil_p/2 being undefined:defmodule LiveViewAppWeb.NavComponents do use Phoenix.Component use LiveViewAppWeb, :verified_routes def main(assigns) do ~H"""...use LiveViewAppWeb, :verified_routes adding this fixed it.

Published 18 days ago
0 likes 77 watched 0 commented
codie
codie

I've been trying sudo apt-get update in Ubuntu 20.04 LTS server, and I was getting the below error.W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'security.ubuntu.com' W: Failed to fetch http://ppa.launchpad.net/certbot/certbot/ubuntu/dists/focal/InRelease Temporary failure resolving 'ppa.launchpad.net' W: Failed to fetch http://rpms.litespeedtech.com/debian/dists/focal/InRelease Temporary failure resolving 'rpms.litespeedtech.com' W: Some index files failed to download. They have been ignored, or old ones used instead.Fix for Temporary failure resolving us.archive.ubuntu.comHere's how I fixed it:Removing bad sources.listsudo rm -rf /etc/apt/sources.list*Adding new good sources.listcd /etc/apt/ wget https://gist.githubusercontent.com/ishad0w/788555191c7037e249a439542c53e170/raw/3822ba49241e6fd851ca1c1cbcc4d7e87382f484/sources.listFixing the Ubuntu's connectionsudo dhclient -v -4 sudo echo 'nameserver 8.8.8.8' > /etc/resolv.confRunning the update commandsudo apt-get updateHooray! you fixed it.

Published 18 days ago
0 likes 81 watched 0 commented
codie
codie

Hey Devs.I started learning Django in COVID times. I was on my grade 12th ATM. I was into content writing and stuff. I had a site, and there was a tool named "Adsense Eligibility Checker" in my site, where user could fill the form, submit their site, and I'd check if there site is eligible or not. The requests were a lot, and task was tedious.I wanted to build something where my presence would not be required. So, I started searching for ways to make a tool/webapp. I was already into learning Python, so maybe fate wanted or something, I got to know about Django.I started learning it. It took me three weeks to nicely learn and 2-3 weeks to develop that tool. I was super happy, wanted to deploy on my cpanel-shared-hosting based server, I had to suffer a lot at that time. But does tons of searches, I deployed it. It got popular, I attached Google Ads on that platform, made a little good money, travelled to places in Nepal.My father's friend used to do Fiverr in Poland, and she suggested me to do so. I had some old Fiverr gigs added, but removed them (were primarily for Photoshop editing). Add a gig, "I'll deploy Django on cPanel", after 2 days of adding that gig, got an order from a Morroco-ian guy. Delivered it. And my journey boosted.I kept doing indie dev and freelancing. After 2 years, my revenue were not doing so well. Had to apply for a job, got the job, worked there for 5 and a half months, and US-based company named "Blogstorm.AI" offered me a job with comparatively good salary, started working there. It's been 1 and a half years working on blogstorm.Now, I am doing full-time job, indie dev and open source. There's a Django rest API framework, that I am working, called Djapy.IO . The journey is going good. I have migrated all my platforms code to Djapy.I hope to see some new guys in there. Life is good man, so is Django. Have a great day. For suggestions, believe on process.

Zaeemusmani replied 4 months ago
Noce good lovly
Published 4 months ago
3 likes 1664 watched 3 commented
codie
codie

Django Recaptcha v3 will be installed in your Django website soon, just follow my instructions. We will be using Google Recaptcha version 3 ( I am not a robot ) and a Python library django-recaptcha for this task.This is a repost of an old blog of ours.How to install, deploy or integrate Django recaptcha v3?Django recaptcha v3 installationFirst, install django-recaptcha in your virtual workspace.Visit the GitHub Repo of this task/project to get all the source codes.Install and configure django-recaptcha libpip install django-recaptchaNow, add captcha in the INSTALLED_APPS section in your settings.pycaptchaMust be similar to this:INSTALLED_APPS = [ 'captcha', # add this code 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', "your_app_name", # app name ]Recaptcha keys configurationAdd RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY at the last lines of your settings.pyRECAPTCHA_PUBLIC_KEY = 'MyRecaptchaKey123' RECAPTCHA_PRIVATE_KEY = 'MyRecaptchaPrivateKey456'Here, RECAPTCHA_PUBLIC_KEY means SITE KEY and RECAPTCHA_PRIVATE_KEY means SECRET KEY.Now, go to Google reCAPTCHA admin panel and register your site for reCAPTCHA and use those data in the upper code.Cpatcha Form configurationNow, create a file named my_captcha.py and inside it you need to write the following Python codes:from django import forms from captcha.fields import ReCaptchaField class FormWithCaptcha(forms.Form): captcha = ReCaptchaField()Django views configurationWrite a context with "captcha" variable name that with render the form we have coded in FormWithCaptcha which is inside the my_captcha.py .from django.shortcuts import render from .my_captcha import FormWithCaptcha # Create your views here. def home(request): context = { "captcha": FormWithCaptcha, } return render(request, "home.html", context)Rendering recaptcha v3Now, we are rendering recaptcha using the Jinja template method. Use {{captcha}} for rendering the captcha form.For now, have a look here:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <forms> {{captcha}} </forms> </body> </html>Getting data from recaptcha formTo get data from django recaptcha, you need to use g-recaptcha-response.get_recaptcha = request.POST.get("g-recaptcha-response")This will return true if the checkbox is checked, else false.

codie replied 4 months ago
You might also wanna check this video:https://youtu.be/8GNc4Pz4is4
Published 4 months ago
0 likes 664 watched 1 commented
codie
codie

[REPOST] I am a noob at Python in Windows. I am getting env\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. The issue in my Windows machine while activating the virtual environment. Can somebody help me, I am on my Visual Studio CODE.PS C:\Users\USER\Projects\content_idea_gen> env/Scripts/activate env/Scripts/activate : File C:\Users\USER\Projects\content_idea_gen\env\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + env/Scripts/activate + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

khanzain4593 replied 2 months ago
You need to change the PowerShell execution policy to allow scripts to run. Here's how to do it:1. Open PowerShell as Administrator: - Press Win + X and select "Windows PowerShell (Admin)".2. Set the Execution Policy: - Run this command to allow local scripts to run: ```powershell Set-ExecutionPolicy RemoteSigned ```3. Confirm the Change: - Type Y and press Enter to confirm.4. Activate the Virtual Environment: - Close and reopen PowerShell or your VS Code terminal. - Activate your virtual environment with: ```powershell .\env\Scripts\Activate.ps1 ```This should resolve the issue and allow you to activate your virtual environment.For better understanding, here are the some excellent learning platforms.1. W3 School 2. Iqra Technology 3. JavatPoint
Published 5 months ago
0 likes 783 watched 2 commented
codie
codie

Deploying SvelteKit can be a tricky job, especially when you're doing it for the first time, unlike me. But, after a few deployments, you get used to with it. So, today, I am guiding you through it, and giving you exact ways to deploy and run your SvelteKit in the server.Also, will be talking about something that I personally developed, called svelte-deploy command.SvelteKit configurationFirst of all, you need a module to be installed in your sveltekit project, @sveltejs/adapter-node;npm install @sveltejs/adapter-nodeNow, you have to configure your svelte.config.js, by adding replacing that upper module with @sveltejs/adapter-auto;import adapter from '@sveltejs/adapter-node'; // ^ // replace @sveltejs/adapter-auto | const config = { preprocess: [ vitePreprocess(), preprocess({ postcss: true }) ], ...Now, your SvelteKit setup is almost done.Server setupIf you have installed git in your Lightsail server then what you have to do is, clone your repo to a specific folder.Creating a service setupNow, we need to create a service setup /etc/systemd/system/<your_svelte_service_name>.service:[Unit] Description=SvelteApp After=network.target [Service] User=root Group=www-data WorkingDirectory=/home/ubuntu/svelte-apps/<path-to-your-app> ExecStart=/home/ubuntu/.nvm/versions/node/v19.8.0/bin/node /home/ubuntu/svelte-apps/<path-to-your-app>/build/index.js Environment=PORT=3099 ORIGIN=https://yoursite.com [Install] WantedBy=multi-user.targetMake sure you have installed NodeJs, suggested to installed it using nvm. You can see learn about installing nvm here.And to checkout the path of your current running node, use whereis node, and paste your preferred node to ExecStart=path_to_node path_to_index.jsNginx Setupupstream sveltekit-frontend { server localhost:3099; } server { listen 80; listen [::]:80; set $app_dir "/home/ubuntu/svelte-apps/<path-to-your-app>"; # not required server_name yoursite.com; location / { proxy_pass http://sveltekit-frontend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }Now, run;sudo nginx -tIf result is:nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successfulIt's usually good, and you're ready to go. (if not, ask below, happy to help)Restart the servicesNow, restart the nginx and the created services:sudo systemctl enable <your_svelte_service_name>.service; sudo systemctl start <your_svelte_service_name>.service; sudo systemctl restart nginx.service;Done, now your site is up and running. Lemme know if you need help.Have a good day 😊

Published 5 months ago
3 likes 900 watched 0 commented
aryangupta001

Q2: Let A[n] be an array of n distinct integers. If i < j and A[i] > A[j], then the pair (i, j)is called an inversion of A. Write a C/C++ program that determines the number ofinversions in any permutation on n elements in O(n lg n) worst-case time.(Hint: Modify merge sort)Example: A = {4, 1, 3, 2} output is 4program:-#include<stdio.h> int total_inversions(int arr[], int n, int count); int main(){ int arr[] = {4, 1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); int count = 0; count = total_inversions(arr, n , count); printf("%d", count); return 0; } int total_inversions(int arr[], int n, int count){ for(int i = 0; i < n-1; i++){ for(int j = i+1; j < n; j++){ if (arr[i] > arr[j]){ count++; } } } return count; }

somesh replied 5 months ago
Hey buddy!As you well know, the program you've suggested here is great, reliable but there's a certain hitch. It runs with a time complexity of O(n^2). This might not seem like a big deal but when you're dealing with huge arrays with a large number of elements, it may cause performance issues. The challenge here is finding the number of inversions in a permutation on n elements, but the trick is doing it within O(nlogn) worst-case time. A smart modification of the merge sort algorithm can be a real game changer here, and lucky for you, I tweaked it to our benefit here: #include<stdio.h> int merge(int arr[], int temp[], int left, int mid, int right); int _mergeSort(int arr[], int temp[], int left, int right); int inversionCount(int arr[], int n) { int temp[n]; return _mergeSort(arr, temp, 0, n - 1); } int _mergeSort(int arr[], int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { mid = (right + left)/2; inv_count = _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid+1, right); inv_count += merge(arr, temp, left, mid+1, right); } return inv_count; } int merge(int arr[], int temp[], int left, int mid, int right) { int i, j, k; int inv_count = 0; i = left; j = mid; k = left; while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count = inv_count + (mid - i); } } while (i <= mid - 1) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (i=left; i <= right; i++) arr[i] = temp[i]; return inv_count; } int main(int argv, char **args) { int arr[] = {4, 1, 3, 2}; printf("Number of inversions are %d \n", inversionCount(arr, sizeof(arr)/sizeof(arr[0]))); return 0; } As you asked, this piece of code will help you get the inversion count in an array and that too in O(nlogn) time complexity, thanks to merge sort.And before I close the lid on this one, I hope you are doing alright handling the pointers and recursion here. The inversion counting part is the only trick in this pudding. Remember, for every i in the first array, if j is an element in the second array, all elements after i in the first subarray and j in the second subarray will form valid inversions. So simply add up (mid – i) inversions for all such valid i and j!This should help you out. Let me know if you need me to break it down more.Happy Coding!
Published 5 months ago
0 likes 517 watched 1 commented
tej
tej

I tried to create a simple embed HTTP server in Rust using Actix Web for the Desktop Tauri app. However, I found a lot of problems and unable to perform a simple file upload operation and have Websocket without extra dependencies. None of the stackoverflow solution worked for me.May be I am a Django Guy and found difficult to use Actix Web which may be my skill issue. So I thought to create my own web framework in Rust for embed projects.Here's github link: https://github.com/tejmagar/rusty-web/It is same like a Django's functional approach.use rusty_web::paths::{Path, Paths}; use rusty_web::request::Request; use rusty_web::response::Response; use rusty_web::server::run_server; fn home(request: Request, mut response: Response) { response.html(200, "Home Page".to_string()).send(); } fn about(request: Request, mut response: Response) { response.html(200, "About Us".to_string()).send(); } fn main() { let paths: Paths = vec![ Path::new("/".to_string(), home), Path::new("/about/".to_string(), about), ]; run_server("0.0.0.0:8080", paths); }I will be using this for my personal projects. Leave your suggestions to make this project better.

codie replied 6 months ago
Use struct.struct XYZStruct { var_1: String var_2: String var_3: AnotherStruct } struct AnotherStruct {}and something like this:fn about(request: Request, mut response: Response) { response.json(Status::Ok, XYZStruct::new(...)).send(); }
Published 6 months ago
1 likes 1093 watched 4 commented